在一个表中查询查找重复值

时间:2018-12-10 20:31:40

标签: mysql

我正在尝试编写一个查询,该查询将5个在不同电子邮件中键入的用户ID进行比较,然后,如果它找到在同一电子邮件中键入两个不同的用户ID,则它将在同一列中显示使用的电子邮件,以及其他5个列显示了两个用户都使用了它的次数,未在单个表中使用它的其他3个用户则为0。这是一个例子:

输入:

    IDs|Email              
    U1 |test@email        
    U2 |test1@email
    U1 |test1@email
    U3 |test1@email
    U4 |test@email
    U1 |test1@email

输出:

    Email        | ID1 | ID2 | ID3 | ID4 | ID5
    test@email   |  1  |  0  |  0  |  1  |  0
    test1@email  |  2  |  1  |  1  |  0  |  0

ive一直在尝试这样做,并尝试了所有可能的选项,例如GROUP BY和self-JOIN,但它没有运行查询。 请帮我。提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用lemma cond_proof : "∃ point1 point2 . cond point1 point2 = True" sledgehammer by (metis Point_ext_def abs_division_segment add_diff_cancel_left' cond_def select_convs(1)) Metis: Unused theorems: 您的结果:

conditional aggregation

由于您使用的是pivot,因此还可以使用以下较短的版本:

select email, 
       sum(case when ids = 'U1' then 1 else 0 end) as Id1,
       sum(case when ids = 'U2' then 1 else 0 end) as Id2,
       sum(case when ids = 'U3' then 1 else 0 end) as Id3,
       sum(case when ids = 'U4' then 1 else 0 end) as Id4,
       sum(case when ids = 'U5' then 1 else 0 end) as Id5
from yourtable
group by email

答案 1 :(得分:0)

此查询应执行以下操作:

select
  email,
  sum(case ids = 'U1' then 1 end) as id1,
  sum(case ids = 'U2' then 1 end) as id2,
  sum(case ids = 'U3' then 1 end) as id3,
  sum(case ids = 'U4' then 1 end) as id4,
  sum(case ids = 'U5' then 1 end) as id5
from my_table
group by email