Joins from same table - php

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.

Related

Combine two mysql group queries

I'm making a web app to create tournaments and as i have learned PHP in the course of this project, so my skills aren't probably the best.
I have an identifier in my database day2_semifinal or day2_additional which basically identifies the type of semifinal.
So my first query is:
$numberquery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (resultid=r_id)
INNER JOIN players
ON (p_id=playerid)
INNER JOIN tourneys
on (T_Id=tourneyid)
WHERE tourneyid='$tourneyid' and
in_day2 = 1 and
day2_semifinal IS NOT NULL
GROUP BY day2_semifinal
ORDER BY agegroupid",$connection);
This will get me all the semifinal groups, i'll iterate over them and query all the players in group:
$semigroup = $group['day2_semifinal'];
$playerQuery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (r_id=resultid)
INNER JOIN players
on (p_id=playerid)
WHERE tourneyid='$tourneyid' AND
day2_semifinal = '$semigroup' and
in_day2 = 1
ORDER BY day2startplace",$connection);
Now after i've created tables and echoed all the data from player queries for day2_semifinal, i run another query:
$numberquery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (resultid=r_id) INNER JOIN players ON (p_id=playerid) WHERE tourneyid='$tourneyid' and in_day2 = 1 and day2_additional_nosemi IS NOT NULL AND day2_additional_nosemi <> 0 GROUP BY day2_additional_nosemi ORDER BY agegroupid",$connection);
Which is fairly similar to the first one, only thing different is day2_semifinal identifiers have changed to day2_additional. After that query, i'll again, iterate over the day2_additional_nosemi groups and query the players inside of them:
$additionalgroup = $group['day2_additional_nosemi'];
$playerQuery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (r_id=resultid) INNER JOIN players on (p_id=playerid) WHERE tourneyid='$tourneyid' AND day2_additional_nosemi = '$additionalgroup' and in_day2 = 1 ORDER BY day2startplace",$connection);
Now this works, but this creates an issue with ordering, since the first query orders them by agegroupid but only for players in day2_semifinal (and i'd like to have day2_additional players ordered together with day2_semifinal). If i run another query the previous data has already been echoed and ordering is not right. How could i concatenate two $numberquery queries in order to select players after them as well?
I'm answering my own question as i figured out a way to do this. What i did, was removed ORDER BYfrom both queries and created a new query which concatenated the two with UNION:
SELECT * FROM (
SELECT *
FROM tourneyplayers as tp1
INNER JOIN results as r1
on (tp1.resultid=r1.r_id)
INNER JOIN players as p1
ON (p1.p_id=tp1.playerid)
WHERE tp1.tourneyid=96 and
tp1.in_day2 = 1 and
r1.day2_semifinal IS NOT NULL
GROUP BY r1.day2_semifinal
UNION ALL
SELECT *
FROM tourneyplayers as tp2
INNER JOIN results as r2
on (tp2.resultid=r2.r_id)
INNER JOIN players as p2
ON (p2.p_id=tp2.playerid)
WHERE tp2.tourneyid=96 and
tp2.in_day2 = 1 and
r2.day2_additional_nosemi IS NOT NULL AND
r2.day2_additional_nosemi <> 0
GROUP BY r2.day2_additional_nosemi
) t ORDER BY t.agegroupid;

SQL left join only returns one row instead of many

I have a two tables, one with cities (id and city name) and one with pictures of the cities (city_id, etc).
Let's say I'm looking for the city called Sibiu. That should return 3 results, since there are more cities like that in the table (Miercurea Sibiului, Sibiu, Poiana Sibiului), but it only returns one.
Also, as a note, the timeline_elements doesn't have any pictures of the city yet.
SELECT cities_countries.*, COUNT(timeline_elements.city_id) as number_of_photos
FROM cities_countries
LEFT JOIN timeline_elements on (cities_countries.id = timeline_elements.city_id)
WHERE cities_countries.name LIKE '%Sibiu%'
Add the GROUP BY and also explicitly mention all the column names for the cities_countries table. I consider these are the columns in the cities_countries table. id, city_id, city_name.
Also set alias name for each table for the better readability.
SELECT C.id, C.city_id, C.city_name, ....
, COUNT(T.city_id) as number_of_photos
FROM cities_countries C
LEFT JOIN timeline_elements T ON C.id = T.city_id
WHERE C.name LIKE '%Sibiu%'
GROUP BY C.id, C.city_id, C.city_name, ....

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.

select only specific fields in joined table

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

Inner join 2 tables with same column names

I'm working on displaying the achievements from my minecraft server on my website.
But I can't get it to work.
function achievements() {
global $id;
$sql="SELECT * FROM achievements
INNER JOIN stats ON achievements.type=stats.type
INNER JOIN stats ON achievements.block=stats.block
INNER JOIN stats ON achievements.data=stats.data
INNER JOIN stats ON achievements.value=stats.value
WHERE player_id = $id";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
}
Will I be able to use $rows['achievements.type']; and $rows['stats.type']; to get the column "type" from the selected table, or is there a another way to do it?
The column and table names are defined by the plugin I use, so the names can't be changed.
the reason why it is not working is because (in my own opinion) the server is a little confused on where how it will handle the columns names properly. In order it to work, add an alias on every table that has the same name that you want to join as well as the columns, eg
SELECT achievements.*,
a.Name as TypeName,
b.Name AS BlockName,
c.Name as DataName,
d.Name AS ValueName
FROM achievements
INNER JOIN stats a ON achievements.type = a.type
INNER JOIN stats b ON achievements.block = b.block
INNER JOIN stats c ON achievements.data = c.data
INNER JOIN stats d ON achievements.value = d.value
WHERE player_id = $id
assuming you want to get the names for every specific columns.
Give the columns you want to have while you SELECT them an alias as example:
SELECT `achievements`.`type` AS `Achieve-Type`
FROM `achievements`
Now you can get the values like this: $rows['Achieve-Type'];
It's easier to alias the table names with...
SELECT * FROM achievements AS ac INNER JOIN stats as st
You could even alias the results if you wish so they are more distinguishable when you select them from $rows

Categories