select only specific fields in joined table - php

Is it possible to specify the fields that you want in the left joined table
i.e.
SELECT * FROM students
LEFT OUTER JOIN classes_enrolled (can i specify fields here)
ON students.student_id = classes_enrolled.student_id
I only wanted to get the field subject from the left joined table classes_enrolled rather than have * all the fields in the matched rows being appended

Why not try something like
SELECT students.* ,
classes_enrolled.subject
FROM students LEFT OUTER JOIN
classes_enrolled ON students.student_id = classes_enrolled.student_id
It is actually considered good practice to specify the field names, rather than using SELECT *

you can choose any of these:
using subquery to select on specified columns,
SELECT *
FROM students
LEFT OUTER JOIN
(
SELECT student_id, subject
FROM classes_enrolled
) b ON students.student_id = b.student_id
or the one that I prefer -- to manually select these columns,
SELECT students.*,
classes_enrolled.subject
FROM students
LEFT OUTER JOIN classes_enrolled
ON students.student_id = classes_enrolled.student_id

Try below
SELECT , classes_enrolled.subject, * FROM students,
LEFT OUTER JOIN classes_enrolled
ON students.student_id = classes_enrolled.student_id

Related

UNION SELECT tables with inner join and GET values with "ALIAS"

So I have 2 tables, Matches and Teams, what I want to do is get some values from the match and Inner join "Teams" to get the names of both teams and add them to a php array later on (getting it all in one sql)
Matches
- IDMatch
- IDLocalTeam
- IDVisitorTeam
- Time
- Half
- Stopped
Teams
- IDTeam
- name
What I have by now is
$query = "SELECT * FROM `Matches`
INNER JOIN `Teams` ON `Matches`.IDLocalTeam = `Teams`.IDTeam
UNION SELECT * FROM `Matches` INNER JOIN `Teams`
ON `Matches`.IDVisitorTeam = `Teams`.IDTeam
ORDER BY IDMatch DESC;";
If someone could help me it would be great! Thanks alot
Wouldn't it be easier to use INNER JOIN twice? It's definitely quicker way of doing joins anyway.
SELECT
*
FROM
`Matches` m
INNER JOIN `Teams` t1
ON m.IDLocalTeam = t1.IDTeam
INNER JOIN `Teams` t2
ON m.IDVisitorTeam = t2.IDTeam
ORDER BY
m.IDMatch DESC;
Also start using aliases instead of table names to identify fields in query, it will SQL much smaller.

MySQL join multiple tables error

I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`

Joins from same table

I am trying to join the same table "travel_plan" twice, as the value(s) I need are location_from & location_to in which I can then join the value to my cities table to grab the city name.
SELECT * FROM travel_plan
LEFT JOIN Cities ON Cities.CityID = travel_plan.location_to AS plan_to
LEFT JOIN Cities ON Cities.CityID = travel_plan.location_from AS plan_from
LEFT JOIN user_table ON travel_plan.user_id = user_table.id
ORDER BY date_from DESC LIMIT 0,4") or die(mysql_error());
You need to use table aliases correctly when you're joining the same table more than once, as you're doing with Cities in this query.
SELECT *
FROM travel_plan AS tr
LEFT JOIN Cities AS C1 ON C1.CityID = tr.location_to
LEFT JOIN Cities AS C2 ON C2.CityID = tr.location_from
LEFT JOIN user_table AS us ON tr.user_id = us.id
ORDER BY date_from DESC
LIMIT 0,4
The way you wrote your query, the LEFT JOIN AS clauses were misplaced and not used for qualifying the column names.
This use of SELECT * is really suboptimal, however. From this four-table JOIN, SELECT * kicks back lots of columns with duplicate names, which fouls up _fetch_assoc() methods in php.
Your best bet is to enumerate the columns you fetch, and provide aliases so they don't end up with the same names. I don't know the names of your columns so I have to guess, but it would go something like this.
SELECT us.name, us.id AS userid,
C1.cityname AS to_cityname,
C2.cityname AS from_cityname,
FROM ....
Then you'll find the values in $result['from_cityname'] after you fetch each row.
You misuse the AS keyword, it can be only used in the select part of the query (before FROM), or optionally as alias for table references. But not in the ON part of a join. I guess what you want is:
SELECT *, c1.City as toCity, c2.City as fromCity FROM travel_plan LEFT JOIN Cities c1 ON c1.CityID = travel_plan.location_to LEFT JOIN Cities c2 ON c2.CityID = travel_plan.location_from LEFT JOIN user_table ON travel_plan.user_id = user_table.id ORDER BY date_from DESC LIMIT 0,4
Now you can access the the column aliases toCity and fromCity in your resultset, even though the the original column names are the same.

One query for two similar tables

i have two similar tables with the same fields. One is named "input" and the other "product" Both have the same fields:
id
name
qty
unit
price
pic(fk - both reference the same picture table).
My first question is: How can I write a query to effectively return every item's details?
I use JSON to export this data
$json_data.='"rec'.$i.'":{"id":"'.$id.'", "name":"'.$name.'", "price":"'.$price.'", "qty":"'.$qty.'", "units":"'.$unit.'", "pic":"'.$pic.'"},';`.
My second question is: How can i effectively reference all the fields from both tables?
NB: I don't want to put the two categories in the same table, say, item, with an additional field of category.
Just to rectify this: the join condition is (input.pic=pic.id and product.pic=pic.id)
If the tables have the same fields you can do a union query.
SELECT * FROM table1
UNION
SELECT * FROM table2
if they are similar as you say then you just have to select the fields that they have in common into both
SELECT "table1" as source_table,id,name,qty,unit,price from table1
UNION
select "table2" as source_table,id,name,qty,unit,price from table2
note I added a source table so you could backtrack where you got it from .... cheers
use full outer in mysql this is not possible so use/take help of union to get all the rows
SELECT * FROM input
LEFT JOIN product ON input.id = product.id
UNION
SELECT * FROM input
RIGHT JOIN product ON input.id = product.id
The problem with that scenario is the word "efficient". The pic - input - leftjoin is slower than an inner join, and the sum of left join input and left join output is slower than a simple inner join.
select pic.id as pic_id, pic.data, input.id as input_id, output.id as output_id, input....
from pic
left join input on input.pic_id = pic.id
left join output on output.pic_id = pic.id
or
select pic.id as pic_id, pic.data, "input" as type, input.id as id, input....
from pic
inner join input on input.pic_id = pic.id
union
select pic.id as pic_id, pic.data, "output" as type, output.id as id, output....
from pic
inner join output on output.pic_id = pic.id
The union is faster, because of using two inner joins.

How to select from two tables and display only what you want?

I want to select from 2 different tables.
In the first table I want to select all, but I will display only what I want.
In the second table I want to select only the profile picture but even if a user does not have a profile picture his data from the user table should be selected.
I am using inner joins. Below is my code:
SELECT * FROM
tish_user INNER JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
To select from two different tables, you should specify values from each table that you want, not using catch-all *. Using a LEFT JOIN instead of an INNER JOIN lets you connect the tables you are querying from on a single point. You can query any kind of relationship between the tables at that point.
This query will give you all the userids in tish_user returning the matching tish_images.prof_image record if prof_image is 1, NULL otherwise.
SELECT
tish_user.user_id,
tish_images.prof_image
FROM
tish_user
LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
AND tish_images.prof_image = 1
Try this way
SELECT * FROM
tish_user, tish_images
WHERE tish_user.user_id = tish_images.user_id
AND
tish_images.prof_image = 1;
I think this might help you.
Cheers bro
Use LEFT JOIN instead of INNER JOIN.
SELECT * FROM
tish_user LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
Explanation
LEFT JOIN selects all rows in the left table, even if there are no entries in the right table (in which case the columns for the right table will be NULL)
Also check RIGHT JOIN, it does the same thing with the right side :)
Try this:
SELECT *
FROM
tish_user U
LEFT JOIN tish_images I
ON U.user_id = I.user_id
AND = I.prof_image = 1
Try this:
Suppose you want to display the userID, firstname and lastname from the tish_user table and the prof_image from the tish_images table.
SELECT tish_user.userd_id, tish_user.firstname, tish_user.lastname, tish_images.prof_image
FROM tish_user tish_user LEFT JOIN tish_image tish_image ON tish_user.user_id=tish_images.user_id WHERE tish_image.prof_image=1
I think this will do.

Categories