使用mysql,查找具有1个值而不是另一个值的ID列表

时间:2017-05-20 15:35:17

标签: mysql

enter image description here

在此表中,每个receipt_id都有多个status_codes。如何提取状态代码为“A”但不是“B”的receipt_id列表。

2 个答案:

答案 0 :(得分:0)

您可以使用NOT EXISTS,例如:

SELECT t.receipt_id
FROM table t
WHERE t.status_code = 'A'
AND NOT EXISTS (
   SELECT receipt_id FROM table WHERE receipt_id = t.receipt_id AND status_code = 'B'
);

答案 1 :(得分:0)

receipt_id分组,只收集那些按条件分组的群组

select receipt_id
from your_table
group by receipt_id
having sum(status_code = 'A') > 0
   and sum(status_code = 'B') = 0