tags:

views:

77

answers:

2

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.

+1  A: 

check this out

for id_reservation = 1

select ml.id_meal  as id_meal,id_Reservation,name,pcs  from  Meal as ml
left outer join 
(select IFNULL(id_Reservation,1) as  id_Reservation, pieces as pcs,id_meal from    Reservation_has_meal    where id_reservation=1) rm
on  rm.id_meal  =  ml.id_meal

for id_reservation = 2

select ml.id_meal  as id_meal,id_Reservation,name,pcs  from  Meal as ml
left outer join 
(select IFNULL(id_Reservation,2) as  id_Reservation, pieces as pcs,id_meal from    Reservation_has_meal    where id_reservation=2) rm
on  rm.id_meal  =  ml.id_meal

i.e you require to replace id_reservation value with the value you are using for searching

Pranay Rana
Thanks for reply.Mysql returns warning: #1582 - Incorrect parameter count in the call to native function 'isnull'And in few words, I want to limit the result with id_reservation and at the same time I want to select all results from table Meal.=> I want to select each row from Meal and left join the pieces from reservation_has_meals where id_reservation has some specific id_reservation.Thanks.
Jaroslav Moravec
check now error of isnull is resolved
Pranay Rana
Yes. Thanks. But the result is not what I need because the condition at the end of query cause that results contains only rows, where id_reservation exists - so with id 2 I get empty result and with id 1 I get only 2 rows instead of 3.
Jaroslav Moravec
try this its working or not
Pranay Rana
inform its working or not
Pranay Rana
Yes, that works with one correction: in inner select we have to select also column id_meal. After this correct I accepted the answer. Thank you.
Jaroslav Moravec
to accept answer you have to select right sign and have to up vote it anyways its good practical question.
Pranay Rana
I know, please correct the query before a I'll do it:select ml.id_meal as id_meal,id_Reservation,name,pcs from Meal as mlleft outer join (select IFNULL(id_Reservation,2) as id_Reservation, pieces as pcs, id_meal from Reservation_has_meal where id_reservation=2) rmon rm.id_meal = ml.id_meal
Jaroslav Moravec
added id_meal in inner query
Pranay Rana
A: 

I have some solution, but I don't want to use inner select and if I would have more data in tables the query is very ugly.

SELECT *, (SELECT pcs FROM Reservation_has_meal WHERE Reservation_has_meal.id_meal = meal.id_meal AND Reservation_has_meal.id_reservation=1) FROM meal

Jaroslav Moravec