jmarais
jmarais

Reputation: 93

mysql left join with 2 foreign keys

Maybe there is a simple solution but I simply cannot get this resolved for a long time. I looked at the examples here but it does not seems to resolve my problem.

I have 2 tables where I use a left join:

SELECT b.box_id, b.address_id, b.tracking, b.value, ad.city, ad.street_nr, ad.country, ad.postcode
FROM boxes AS b,
LEFT JOIN addresses AS ad ON ad.id = b.address_id
WHERE b.box_id = 414 AND b.box_id = 415 

From this I need a returned result that contains 2 rows, one for box_id 414 and one for box_id 415.

In other words, I need a row that contains the values of b.box_id, b.address_id, b.tracking, b.value, ad.city, ad.street_nr, ad.country, ad.postcode for both ids 414 and 415

The query above obviously won't work. I tried UNION but that does not work either. Any help would be appreciated.

Upvotes: 0

Views: 119

Answers (1)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

Just switch your AND to OR

SELECT b.box_id, b.address_id, b.tracking, b.value, ad.city, ad.street_nr, ad.country, ad.postcode
FROM boxes AS b
LEFT JOIN addresses AS ad ON ad.id = b.address_id
WHERE b.box_id = 414 OR b.box_id = 415

Upvotes: 2

Related Questions