SQL独特的问题

时间:2015-07-10 13:32:51

标签: mysql sql error-handling

我有一个看起来像的SQL表:

email               User_id Category
Sarah.Ackland@bmo.com   44  Sent_Emails
Sarah.Ackland@bmo.com   44  Sent_Emails
Sarah.Ackland@bmo.com   44  Undeliverable
Adam.Bus@wraglaw.com    50  Sent_Emails
Adam.Duthie@bevn.com    56      Sent_Emails

我正在寻找一个看起来像的输出:

email               User_id Category
Adam.Bushby@wraglaw.com 50  Sent_Emails
Adam.Duthie@bevn.com    56      Sent_Emails

主要是我不希望看到任何被标记为“未送达”的电子邮件。我尝试使用distinct,但仍然给了我一行“Sarah.Ackland@bmo.com”

Appreciate your support.

Thanks

3 个答案:

答案 0 :(得分:3)

我想你想要这样的东西:

select t.*
from sqltable t
where not exists (select 1
                  from sqltable t2
                  where t2.email = t.email and t2.category = 'Undeliverable'
                 );

答案 1 :(得分:3)

试试这个sql语句:

SELECT * FROM your_table WHERE User_id NOT IN (SELECT User_id FROM your_table WHERE Category = 'Undeliverable')

答案 2 :(得分:1)

SELECT DISTINCT * 
FROM your_table 
WHERE User_id NOT IN (SELECT User_id FROM your_table WHERE Category = 'Undeliverable')