4

这是查询

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。

4

1 回答 1

1

怎么样:

group_concat(distinct em.customer_id)

count(distinct em.customer_id)
于 2012-11-09T18:12:09.920 回答