2012-11-09 22 views
4

这是查询越来越明显重复的ID的数量在mysql中

select count(*), 
     ss.pname, 
     ttu.user_id, 
     ttl.location_name , 
     group_concat(em.customer_id), 
     count(em.customer_id) 
    from seseal as ss, 
     track_and_trace_user as ttu, 
     track_and_trace_location as ttl, 
     eseal_mapping as em 
where ss.real_id=em.e_id 
    and em.user_id=ttu.user_id 
    and ttu.location_id=ttl.location_id 
group by ss.pname, ttu.user_id, ttl.location_name 
having count(em.customer_id)>1 ; 

和下面的结果:

+----------+----------------+---------+---------------+------------------------------+-----------------------+ 
| count(*) | pname   | user_id | location_name | group_concat(em.customer_id) | count(em.customer_id) | 
+----------+----------------+---------+---------------+------------------------------+-----------------------+ 
|  6 | Nokia N91  |  1 | Malad   | 60,51,60,51,58,58   |      6 | 
|  2 | SUPERIA 1000gm |  4 | Raichur  | 51,46      |      2 | 
|  5 | SUPERIA 1000gm |  5 | west bengal | 51,46,51,51,46    |      5 | 
|  2 | SUPERIA 500gm |  4 | Raichur  | 59,59      |      2 | 
|  3 | SUPERIA 500gm |  5 | west bengal | 59,46,59      |      3 | 
+----------+----------------+---------+---------------+------------------------------+-----------------------+ 

现在的问题是,你可以在结果集中看到,在某些行的第二列最后一列customer_ids重复,并且在某些行中是唯一的。最后一栏列出了它的数量。
现在我想要选择第三行,有两个客户ID即51和46,这些在该行中重复,所以我最后一列此行应包含2.
类似于最后一行我的最后一列应该包含1,因为只有一个customer id是重复的,即59.
因此,如果你明白确切的问题,那么第二行不应该是该结果集的一部分,因为它不包含任何重复的客户ID。

+0

你想从各行的不同em.customer_Id? ie:60,51,60,51,58,58 - > 60,51,58 |||| 51,46 - > 0 – G21

回答

1

如何:

group_concat(distinct em.customer_id) 

count(distinct em.customer_id)