我有一个类似的查询:
select gift.id as giftId, gift.title, count(vouchercode.id) as stock, vouchertemplate.unlimited, gift.voucherTemplate, vouchertemplate.id as voucherId,vouchertemplate.title
from gift
inner join vouchertemplate
left join vouchercode
on gift.voucherTemplate = vouchertemplate.id
on vouchertemplate.id = vouchercode.template
and vouchercode.given = 0
where gift.id in (13,14,15,16)
我发现这并没有给我正确的结果。看来on gift.voucherTemplate = vouchertemplate.id
无效。我在左连接之前需要它。所以我不能把所有的连接条件放在一起?我指定了要加入的表名和列,所以我想知道为什么会有区别?
以下查询为我提供了正确的结果。
select gift.id as giftId, gift.title, count(vouchercode.id) as stock, vouchertemplate.unlimited, gift.voucherTemplate, vouchertemplate.id as voucherId,vouchertemplate.title
from gift
inner join vouchertemplate
on gift.voucherTemplate = vouchertemplate.id <<< DIFF HERE
left join vouchercode
on vouchertemplate.id = vouchercode.template
and vouchercode.given = 0
where gift.id in (13,14,15,16)