简体   繁体   English

MySQL将多行查询连接到一列

[英]MySQL join multiple rows of query into one column

Table structure 表结构

client_commands (the "main" table): client_commands (“主”表):
id | completed

command_countries : command_countries
id | command_id | country_code

command_os : command_os
id | command_id |OS

command_id on references the id column on client_commands. on的command_id引用client_commands的id列。

Problem 问题

I can add client commands with filters based on countries and operating systems. 我可以添加带有基于国家和操作系统的过滤器的客户端命令。 To try and normalise my DB structure, for each new command added: 要尝试规范我的数据库结构,请为每个添加的新命令:

  • Add a new row to client_commands 向client_commands添加新行
  • For each country, I add a new row to command_countries, each referencing client_command.id 对于每个国家/地区,我都会在command_countries中添加新行,每个行都引用client_command.id
  • For each OS, I add a new row to command_os, each referencing client_command.id 对于每个操作系统,我向command_os添加新行,每个行都引用client_command.id

For one of the pages on my site, I need to display all client_commands (where completed = 0) as well as all the countries and operating systems for that command. 对于我网站上的页面之一,我需要显示所有client_commands(已完成= 0)以及该命令的所有国家和操作系统。 My desired output would be something like: 我想要的输出将是这样的:

id | countries | OS
1  | GB, US, FR| 2, 3
2  | ES, HU    | 1, 3

I'm not sure how to go about doing this. 我不确定该怎么做。 The current query I'm using returns multiple rows: 我正在使用的当前查询返回多行:

SELECT a.id, b.country_code, c.OS
FROM client_commands a
LEFT JOIN command_countries b on b.command_id = a.id
LEFT JOIN command_os c on c.command_id = a.id
WHERE a.completed = 0

查询结果

Any help? 有什么帮助吗?

Thanks! 谢谢!

EDIT: I forgot to mention (if you couldn't infer from above) - there can be a different number of operating systems and countries per command. 编辑:我忘了提(如果您不能从上面推断)-每个命令可以有不同数量的操作系统和国家/地区。

-- -

Also: I know I could do this by pulling all the commands, then looping through and running 2 additional queries for each result. 另外:我知道我可以通过拉出所有命令,然后遍历并为每个结果运行2个附加查询来做到这一点。 But if I can, I'd like to do it as efficiently as possible with one query. 但是,如果可以的话,我想通过一个查询来尽可能有效地做到这一点。

You can do this in one query by using GROUP_CONCAT 您可以使用GROUP_CONCAT在一个查询中执行此操作

SELECT a.id, 
GROUP_CONCAT(DISTINCT b.country_code SEPARATOR ' ,') `countries`, 
GROUP_CONCAT(DISTINCT  c.OS SEPARATOR ' ,') `os`, 
FROM client_commands a
LEFT JOIN command_countries b on b.command_id = a.id
LEFT JOIN command_os c on c.command_id = a.id
WHERE a.completed = 0
GROUP BY a.id

if you want the ordered results in in a row you can use ORDER BY in GROUP_CONCAT like 如果要连续显示有序结果,可以在GROUP_CONCAT使用ORDER BY,例如

GROUP_CONCAT(b.country_code ORDER BY b.command_id DESC SEPARATOR ' ,') `countries`

But be aware of that fact it has a limit of 1024 character to concat set by default but this can be increased b,steps provided in manual 但是请注意,事实上默认情况下,concat最多可设置1024个字符,但是可以增加b, 手册中提供的步骤

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

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