简体   繁体   English

如何在MySQL中查询concat,group_concat,group by的多个子查询

[英]How to query concat, group_concat, group by in MySQL for multiple subquery

I have database structure and data: 我有数据库结构和数据:

id | total | type | value_1 | value_2
1  |   9   |   1  |    10   |    20
2  |   9   |   1  |    21   |    30
3  |   10  |   1  |    31   |    40
4  |   9   |   2  |    41   |    50
5  |   9   |   2  |    51   |    60
6  |   8   |   2  |    61   |    70

Please help me how to query result like this? 请帮我如何查询这样的结果?

type ||  total  ||    id   ||       value
1    ||   9|10  ||  1,2|3  ||  10-20,21-30|31-40
2    ||   9|8   ||  4,5|6  ||  41-50,51-60|61-70

This should work once you change the table name. 更改表名称后,此方法应该可以使用。 The trick is to build it up in stages. 诀窍是分阶段进行构建。 First, combine value_1 and value_2 , then group by total and type using , as a separator, then finally, group by type using | 首先,组合value_1value_2 ,然后使用,作为分隔符按总数和类型分组,最后使用|按类型分组| .

select type,
group_concat(distinct total separator '|') as total, 
group_concat(distinct id separator '|') as id, 
group_concat(distinct vals separator '|') as vals 
from ( select type, 
    group_concat(distinct total separator ',') as total, 
    group_concat(distinct id separator ',') as id, 
    group_concat(distinct vals separator ',') as vals,
    count(id) as count 
    from (select id, total, type, concat(value_1,'-',value_2) as vals from tbl) as a
    group by total, type order by count desc) as b
group by type;

I replicated your table so I could test it out 我复制了您的表,以便可以对其进行测试

mysql> select * from tbl;
+------+-------+------+---------+---------+
| id   | total | type | value_1 | value_2 |
+------+-------+------+---------+---------+
|    1 |     9 |    1 |      10 |      20 |
|    2 |     9 |    1 |      21 |      30 |
|    3 |    10 |    1 |      31 |      40 |
|    4 |     9 |    2 |      41 |      50 |
|    5 |     9 |    2 |      51 |      60 |
|    6 |     8 |    2 |      61 |      70 |
+------+-------+------+---------+---------+
6 rows in set (0.00 sec)

Here is the result. 这是结果。

mysql> select type,
-> group_concat(distinct total separator '|') as total, 
-> group_concat(distinct id separator '|') as id, 
-> group_concat(distinct vals separator '|') as vals 
-> from ( select type, 
-> group_concat(distinct total separator ',') as total, 
-> group_concat(distinct id separator ',') as id, 
-> group_concat(distinct vals separator ',') as vals,
-> count(id) as count 
-> from (select id, total, type, concat(value_1,'-',value_2) as vals from tbl) as a
-> group by total, type order by count desc) as b
-> group by type;
+------+-------+-------+-------------------+
| type | total | id    | vals              |
+------+-------+-------+-------------------+
|    1 | 9|10  | 1,2|3 | 10-20,21-30|31-40 |
|    2 | 9|8   | 4,5|6 | 41-50,51-60|61-70 |
+------+-------+-------+-------------------+
2 rows in set, 1 warning (0.00 sec)

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

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