我有3张桌子:
供应商: ID,姓名,地址,郊区,州,电话,电子邮件,已删除
客户: ID,姓名,地址,郊区,州,电话,电子邮件,已删除
ACCREDITED_SUPPLIERS: id,supplier_id,customer_id
我想显示所有供应商和'标记'已经在ACCREDITED_SUPPLIERS中链接的那些......
以下是我的发言:
SELECT SQL_CALC_FOUND_ROWS s.id, s.name, s.suburb, s.state, s.phone, s.email, s.deleted,
IF( ac.customer_id = '220', 1, 0 ) AS accredited
FROM suppliers s
LEFT OUTER JOIN accredited_suppliers ac ON ac.supplier_id = s.id
WHERE s.deleted='0'
ORDER BY s.name
此语句仅返回10'标记的'行
出于某种原因,它莫名其妙地错过了3行!
SELECT *
FROM `accredited_suppliers`
WHERE `customer_id` = '220'
退货:13行
我的陈述是否正确?我正确地发出了吗?
答案 0 :(得分:1)
你不只是想要:
select
s.id AS supplier_id,
IF(a.supplier_id IS NULL, 0, 1) AS accredited
from
SUPPLIERS s
left join
ACCREDITED_SUPPLIERS a
ON
s.id = a.supplier_id AND
a.customer_id = 220;