Mysql Left join with condition on column

Posted by skicster on Stack Overflow See other posts from Stack Overflow or by skicster
Published on 2010-05-18T08:51:46Z Indexed on 2010/05/18 9:00 UTC
Read the original article Hit count: 172

Filed under:
|

Hi, can you help me with sql query? I have this problem: I have two tables

"Join" table: Reservation_has_meal

+----------------+
| id_reservation |
| id_meal        |
| pieces         |
+----------------+

and table with data: Meal

+-------------+
| id_meal     |
| name        |
+-------------+

Sample data for

Meal:
1 | carrot   
2 | potatoe  
3 | cucumber 

Reservation_has_meal
1 | 2 | 5230
1 | 3 | 1203

How can I get this result for reservation with id_reservation=1:

id_meal | id_Reservation | name      | pcs |
--------------------------------------------
1       | 1              | carrot    | null|
2       | 1              | potatoe   | 5230|
3       | 1              | cucumber  | 1203|
--------------------------------------------

And result for id_reservation = 2:

id_meal | id_Reservation | name      | pcs |
--------------------------------------------
1       | 2              | carrot    | null|
2       | 2              | potatoe   | null|
3       | 2              | cucumber  | null|
--------------------------------------------

Thanks for advice.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about join