在MySQL中获取id不同的那些行

时间:2018-07-23 14:57:53

标签: mysql sql

我有下面提到的表格:

Source     Value     e_mail               count          ID
RT-121     124566    aft.12@hotmail.com   PR12S          P-1
RT-122     124887    efyyhd@hotmail.com                  P-2
RT-123     124887    efyyhd@hotmail.com   PR12S          P-3
RT-124     484566    aft.19@hotmail.com                  P-7
RT-125     484566    aft.19@hotmail.com   PR12S          P-8
RT-126     124566    aft.12@hotmail.com   PR12S          P-1      

我想编写一个查询,给我输出Valuee_mail相同,而CountNull或空白ID的情况。

必需的输出:

Source     Value     e_mail               count          ID
RT-122     124887    efyyhd@hotmail.com                  P-2
RT-123     124887    efyyhd@hotmail.com   PR12S          P-3
RT-124     484566    aft.19@hotmail.com                  P-7
RT-125     484566    aft.19@hotmail.com   PR12S          P-8

1 个答案:

答案 0 :(得分:0)

一种方法使用exists

select t.*
from t
where exists (select 1
              from t t2
              where t2.value = t.value and
                    t2.e_mail = t.e_mail and
                    (t2.count <> '' xor t.count <> '') and
                    t2.source <> t.source
             );

使用(value, e_mail, source)上的索引,这可能是最快的方法。