2012-11-09 74 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)