我有一个名为call_charges
的表,其中包含以下数据。
我需要获得所有未付款的付款,其付款重试也是未付款的。
即transaction_bid
非空且is_paid
0的行将返回id
6和10的记录。
但id
6已付费重试(id
12,retry_id
6和is_paid
1),因此id
6行不应该在结果。
我尝试过:
SELECT `call_charges`.* FROM `call_charges` LEFT JOIN `call_charges` AS
`retries_call_charges` ON `retries_call_charges`.`retry_id` = `call_charges`.`id`
WHERE `call_charges`.`is_paid` = 0 AND (`call_charges`.`transaction_bid` IS NOT NULL
AND `call_charges`.`retry_id` IS NULL)
但它返回id
6和10的行。
提前致谢。
答案 0 :(得分:2)
你走了:
SELECT c1.id
FROM call_charges c1
LEFT JOIN call_charges c2
ON c1.id = c2.retry_id AND c2.is_paid = 1
WHERE c1.transaction_bid IS NOT NULL
AND c1.is_paid = 0
AND c2.id IS NULL
AND c1.retry_id IS NULL
工作小提琴: http://sqlfiddle.com/#!2/29549/13
c2.id
可能为null,因为已使用LEFT JOIN
。如果c1.id
与c2.retry_id
不匹配,则c2.id
将为NULL。