【发布时间】:2013-08-03 14:08:46
【问题描述】:
我有添加剂表:
id name
30 gro
31 micro
32 bloom
33 test
还有stage_additives表:
stage_id additive_id dose
195 30 2
195 31 3
195 32 1
Mysql查询:
SELECT a.id,
a.name,
sa.dose
FROM additives a
LEFT JOIN stage_additives sa
ON sa.stage_id = 195
结果是:
id name dose
32 Bloom 2
32 Bloom 3
32 Bloom 1
30 Gro 2
30 Gro 3
30 Gro 1
31 Micro 2
31 Micro 3
31 Micro 1
33 test 2
33 test 3
33 test 1
这对我来说没有意义,因为即使每个表只有一个具有相同 ID/名称的项目,结果中的每个项目也有 3 个。
我也尝试过内连接,右连接,但结果几乎相同,除了顺序。
我想要的是所有 id、添加剂的名称和 stage_additives 的剂量(如果存在),否则为 NULL(或更好的自定义值 0)
【问题讨论】: