小编Tod*_*odd的帖子

找不到匹配项时,左连接的结果为空

说我有这三个表:

Table: Baskets
id  |  name
1      Sale
2      Premium
3      Standard
4      Expired

Table: Fruit
id  |  name   | basketid
1      Apples    1
2      Oranges   2
3      Grapes    3
4      Apples    2
5      Apples    4

Table: Veggies
id  |  name   | basketid
1      Carrots     1
2      Peas        2
3      Asparagus   1
Run Code Online (Sandbox Code Playgroud)

看起来后两个表应该只是一个称为Produce的表,但是在实际情况下,有充分的理由使它们成为不同的表。我需要编写一个联接,如果购物篮的水果表或蔬菜表中有行,则返回行。我以为我可以通过两个左连接来完成此操作,如下所示:

Select Baskets.*, fruit.name as fruit,
veggies.name as veggies 
from Baskets
left join Fruit on Baskets.id =  Fruit.basketid 
left join veggies on Baskets.id = Veggies.basketid 
where Baskets.id = …
Run Code Online (Sandbox Code Playgroud)

mysql sql

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

mysql ×1

sql ×1