我有这个 sql 查询:
SELECT Customer.IDCustomer, Customer.Name,
Sign.IdSign, Sign.Name, Sign.Delete
FROM Customer
LEFT JOIN Sign_Customer ON Sign_Customer.IDCustomer=Customer.IDCustomer
AND ( SELECT CAST(CASE WHEN S_C.Delete=0 OR S_C.Delete is Null THEN 1 ELSE 0 END AS BIT )
FROM Sign AS S_C
WHERE S_C.IdSign=Sign_Customer.IdSign)=1
LEFT JOIN Sign ON Sign.IdSign=Sign_Customer.IdSign
ORDER BY Customer.Name
此查询工作正常,但我想知道它是否存在另一种方法来过滤我的第一个左连接而不使用 SELECT CAST... 条件。
更新
抱歉,我没有解释我想要的结果:
- 我所有的客户都没有签名
- 我所有启用签名的客户
如果不存在启用标志,则禁用标志的客户不会出现或显示为“无标志”。
示例:我的客户表
IDCustomer | Name
1 | Customer 1
2 | Customer 2
3 | Customer 3
4 | Customer 4
签收客户:
IDCustomer | IdSign
1 | 1
3 | 2
3 | 3
3 | 5
4 | 4
符号
IdSign | Name | Delete
1 | Sign1 | 0
2 | Sign2 | 1
3 | Sign3 | 0
4 | Sign4 | 1
5 | Sign5 | 0
结果
Customer.IDCustomer | Customer.Name | Sign.IdSign | Sign.Name | Sign.Delete
1 | Customer 1 | 1 | Sign1 | 0
2 | Customer 2 | null | null | null
3 | Customer 3 | 3 | Sign3 | 0
3 | Customer 3 | 5 | Sign5 | 0
4 | Customer 4 | null | null | null