mysql左连接有2个外键

时间:2013-01-30 21:03:12

标签: mysql

也许有一个简单的解决方案,但我很长时间都无法解决这个问题。我看了这里的例子,但似乎没有解决我的问题。

我有2个表,我使用左连接:

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 

从这里我需要一个包含2行的返回结果,一行用于box_id 414,一行用于box_id 415.

换句话说,我需要一行包含两个ID的b.box_id,b.address_id,b.tracking,b.value,ad.city,ad.street_nr,ad.country,ad.postcode的值414和415

上面的查询显然不起作用。我试过UNION,但这也不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

只需将AND切换为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