代码之家  ›  专栏  ›  技术社区  ›  Vector JX

获取MySQL中ID不同的行

  •  1
  • Vector JX  · 技术社区  · 7 年前

    我有以下表格:

    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      
    

    我想写一个查询,它给我输出 Value e_mail 都是一样的 Count 或者是 Null 或空白有不同 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 回复  |  直到 7 年前
        1
  •  0
  •   Gordon Linoff    7 年前

    一种方法使用 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) ,这可能是最快的方法。