gpt4 book ai didi

mysql - 在mysql中使用group_concat显示按类别分隔的调查结果

转载 作者:行者123 更新时间:2023-11-29 17:01:08 29 4
gpt4 key购买 nike

在我的 Mysql 数据库中,我有一个名为“survey”的表。该表用于记录调查数据。表结构如下所示。调查在100个以上的不同城市进行。

表名称:调查

ID        options      user_ID   City 
----------------------------------------------
1 option1 101 City1
2 option2 102 City2
3 option3 103 City1
4 option1 104 City3
5 option1 105 City1
6 option2 106 City2
7 option3 107 City1
So on....
------------------------------------------------

我想列出所有选择按城市隔离的特定选项的 user_ids。喜欢

city1
--------------------------------------------
option1 list of user_ids
option2 list of user_ids
option3 list of user_ids

city2
--------------------------------------------
option1 list of user_ids
option2 list of user_ids
option3 list of user_ids
--------------------------------------

so on.......

如果我运行此查询

SELECT OPTIONS,
GROUP_CONCAT(user_id ORDER BY user_id ASC SEPARATOR ' | ' )
FROM survey WHERE city= 'city1'
GROUP BY options ;

我得到了我想要的,但仅限 city1 一个。我如何扩展它以获得所有城市的列表,请帮助,谢谢。

最佳答案

您只需在 select 语句中添加城市,而不是在 where 中,如下所示:

SELECT city,
OPTIONS,
GROUP_CONCAT(user_id ORDER BY user_id ASC SEPARATOR ' | ' )
FROM survey
GROUP BY city, options;

这将导致:

city1 option1  list of user_ids
city1 option2 list of user_ids
city1 option3 list of user_ids
city2 option1 list of user_ids
city2 option2 list of user_ids
city2 option3 list of user_ids

您还可以根据需要在末尾添加:order by options, cityorder by city, options

关于mysql - 在mysql中使用group_concat显示按类别分隔的调查结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52250026/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com