简体   繁体   English

MySQL查询的数量,按分组

[英]MySQL Query with the count, group by

Table: statistics 表:统计

id | user  | Message
----------------------
1  | user1  |message1 
2  | user2  |message2
3  | user1  |message3

I am able to find the count of messages sent by each user using this query. 我能够找到使用此查询的每个用户发送的邮件数。

select user, count(*) from statistics group by user;

How to show message column data along with the count? 如何显示消息列数据和计数? For example 例如

user | count | message
------------------------
user1| 2     |message1
             |message3
user2| 1     |message2    

You seem to want to show Count by user , which message sent by user . 你似乎要显示Countuser ,该message发送的user

If your mysql version didn't support window functions, you can do subquery to make row_number in select subquery, then only display rn=1 users and count 如果您的mysql版本不支持窗口功能,则可以执行子查询以在选择子查询中创建row_number ,然后仅显示rn=1用户并计数

CREATE TABLE T(
   id INT,
    user VARCHAR(50),
  Message VARCHAR(100)
);


INSERT INTO T VALUES(1,'user1'  ,'message1'); 
INSERT INTO T VALUES(2,'user2'  ,'message2');
INSERT INTO T VALUES(3,'user1'  ,'message3');

Query 1 : 查询1

SELECT (case when rn = 1 then user else '' end) 'users',
       (case when rn = 1 then cnt else '' end) 'count',
       message
FROM (
  select 
      t1.user, 
      t2.cnt, 
      t1.message,
      (SELECT COUNT(*) from t tt WHERE tt.user = t1.user and t1.id >= tt.id) rn
  from T t1
  join (
    select user, count(*) cnt
    from T
    group by user
  ) t2 on t1.user = t2.user
) t1
order by user,message

Results : 结果

| users | count |  message |
|-------|-------|----------|
| user1 |     2 | message1 |
|       |       | message3 |
| user2 |     1 | message2 |

You could join the result of your group by with the full table (or vice versa)? 您可以group by全表join group by的结果(反之亦然)?

Or, depending on what you want, you could use group_concat() using \\n as separator. 或者,根据您的需要,可以使用\\n作为分隔符来使用group_concat()

select user, count(*) as 'total' , group_concat(message) from statistics group by user;

Use Group_concat 使用Group_concat

select user, count(0) as ct,group_concat(Message) from statistics group by user;

This will give you message in csv format 这会以csv格式给您消息

NOTE: GROUP_CONCAT has size limit of 1024 characters by default in mysql. 注意:在MySQL中,GROUP_CONCAT的大小限制默认为1024个字符。

For UTF it goes to 1024/3 and utfmb4 255(1024/4). 对于UTF,它将转到1024/3和utfmb4 255(1024/4)。

You can use group_concat_max_len global variable to set its max length as per need but take into account memory considerations on production environment 您可以根据需要使用group_concat_max_len全局变量来设置其最大长度,但要考虑生产环境中的内存注意事项

SET group_concat_max_len=100000000

Update: 更新:

You can use any separator in group_concat 您可以在group_concat中使用任何分隔符

Group_concat(Message SEPARATOR '----')

Try grouping with self-join: 尝试使用自连接进行分组:

select s1.user, s2.cnt, s1.message
from statistics s1
join (
  select user, count(*) cnt
  from statistics
  group by user
) s2 on s1.user = s2.user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM