mysql - 获取 mysql 中不同重复 ID 的计数

标签 mysql count group-by distinct having

这是查询

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 是重复的,而在某些行中是唯一的。最后一列给出了它的计数。
现在我想要的是选择第 3 行,有两个客户 ID,即 51 和 46,并且在该行中重复,所以我这一行的最后一列应该包含 2。
同样,对于最后一行,我的最后一列应该包含 1,因为只有一个 customer id 是重复的,即 59。
因此,如果您了解确切的问题,那么第二行不应是此结果集的一部分,因为它不包含任何重复的客户 ID。

最佳答案

怎么样:

group_concat(distinct em.customer_id)

count(distinct em.customer_id)

关于mysql - 获取 mysql 中不同重复 ID 的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308343/

相关文章:

php - 为什么我的 foreach 循环没有按预期工作?

python - 对于 INSERT,使用带有变量的 LIKE 语句

MySQL 用名字和姓氏字段更新公司字段

mysql - 选择表中不同列的计数

php - 为 PHP echo 计算并显示不同列中的多个 mysql 行值

PHP + MySQL : is it safe to use serialize() without addslashes()?

mysql - 如何获取 MySQL 中嵌套查询和 WHERE IN 使用查询返回的行数?

count - Prolog计数知识库

mysql - 从分组(?)sql查询的第一个排序成员中获取值

pandas - 在两个 pandas 数据帧之间分配值