I'm stuck and I would like your help (advice). I'm using PHP & MysQL and my issue is presented down below.
This is a simplified version of what I'm trying to achieve. I have two tables, one is city list, and the other is departures. I would like to LEFT JOIN these two tables (if possible) and return list of all cities even if there is no connected records in table bus_departures.
Can I do it using just MySQL or I'll have to combine arrays in PHP. I would like to see your solution. Thank you.
City
-------------------
ID | CITY
-------------------
1 | London
2 | Paris
3 | New York
4 | Rome
5 | Zagreb
Bus_Departures
------------------------------------------
ID | DATE | CITY | No. DEPARTURES
------------------------------------------
1 | 2016-06-06 | 1 | 5
2 | 2016-06-06 | 4 | 3
3 | 2016-06-06 | 3 | 2
4 | 2016-06-07 | 3 | 4
5 | 2016-06-07 | 1 | 1
6 | 2016-06-08 | 3 | 8
7 | 2016-06-09 | 1 | 3
8 | 2016-06-09 | 4 | 2
SELECT
SELECT * FROM City LEFT JOIN Bus_Departures WHERE date = '2016-06-07' ORDER BY City ASC
RETURNS
London 2016-06-07 1
New York 2016-06-07 4
I WOULD LIKE TO ECHO THIS TO THE USER
London 2016-06-07 1
New York 2016-06-07 4
Paris 2016-06-07 - (no records at all)
Rome 2016-06-07 - (no records for this day)
Zagreb 2016-06-07 - (no records at all)
Thank you!
left joinhas been performed. As a result, for example your joined Paris entry would look likeParis NULL NULLand not be selected, just like the two Rome entries that will look likeRome 2016-06-06 3andRome 2016-06-09 2.