MySQL结果以逗号分隔的列表


129

我需要运行类似的查询:

SELECT p.id, p.name, 
       (SELECT name 
          FROM sites s 
         WHERE s.id = p.site_id) AS site_list
  FROM publications p

但是我希望子选择返回逗号分隔的列表,而不是数据列。这有可能吗?如果可以,怎么办?

Answers:


250

您可以使用GROUP_CONCAT执行该操作,例如

SELECT p.id, p.name, GROUP_CONCAT(s.name) AS site_list
FROM sites s
INNER JOIN publications p ON(s.id = p.site_id)
GROUP BY p.id, p.name;

10
另外,请快速注意,如果您使用的是PHPMyAdmin,并且想在页面上输出逗号分隔的列表,请使用GROUP_CONCAT(CAST(s.name AS CHAR)),否则它将返回完全无用的内容,例如[BLOB - 20 Bytes]
devios1 2012年

3
目的是好的,MySQL会允许这样做,但是(通常)在使用GROUP BY时要格外小心。选择列表中的项目在GROUP BY子句的上下文中必须是有效的集合。在这种情况下,p.name并非严格有效。任何符合SQL标准的数据库都将其视为错误。对于这种情况,请在选择列表中使用MAX(p.name)或将p.name添加到GROUP BY子句。由于Paul可能意味着p.id代表主键或唯一键,因此将p.name添加到GROUP BY子句不会对最终结果产生影响。
乔恩·阿姆斯特朗

请注意,您可能必须为每个stackoverflow.com/questions/2567000/…
sobelito 2015年

非常感谢!你帮了我很多忙!
安德烈的Agostinho

11

除了使用,group concat()您可以只使用concat()

Select concat(Col1, ',', Col2) as Foo_Bar from Table1;

编辑这仅适用于mySQL; Oracle concat仅接受两个参数。在oracle中,您可以使用诸如select col1 ||','|| col2 ||','|| col3之类的东西作为来自table1的foobar;在sql server中,您将使用+代替管道。


2
这在GROUP BY情况下不起作用,而GROUP_CONCAT()将串联单个列的内容
Aram Paronikyan

5

现在只有我遇到这种情况,并发现了一些更有趣的功能GROUP_CONCAT。希望这些细节会让您感到有趣。

简单的GROUP_CONCAT

SELECT GROUP_CONCAT(TaskName) 
FROM Tasks;

结果:

+------------------------------------------------------------------+
| GROUP_CONCAT(TaskName)                                           |
+------------------------------------------------------------------+
| Do garden,Feed cats,Paint roof,Take dog for walk,Relax,Feed cats |
+------------------------------------------------------------------+

GROUP_CONCAT和DISTINCT

SELECT GROUP_CONCAT(TaskName) 
FROM Tasks;

结果:

+------------------------------------------------------------------+
| GROUP_CONCAT(TaskName)                                           |
+------------------------------------------------------------------+
| Do garden,Feed cats,Paint roof,Take dog for walk,Relax,Feed cats |
+------------------------------------------------------------------+

GROUP_CONCAT,DISTINCT和ORDER BY

SELECT GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC) 
FROM Tasks;

结果:

+--------------------------------------------------------+
| GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC) |
+--------------------------------------------------------+
| Take dog for walk,Relax,Paint roof,Feed cats,Do garden |
+--------------------------------------------------------+

GROUP_CONCAT与DISTINCT和SEPARATOR

SELECT GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ') 
FROM Tasks;

结果:

+----------------------------------------------------------------+
| GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ')                |
+----------------------------------------------------------------+
| Do garden + Feed cats + Paint roof + Relax + Take dog for walk |
+----------------------------------------------------------------+

GROUP_CONCAT和合并列

SELECT GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ') 
FROM Tasks;

结果:

+------------------------------------------------------------------------------------+
| GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ')                                 |
+------------------------------------------------------------------------------------+
| 1) Do garden 2) Feed cats 3) Paint roof 4) Take dog for walk 5) Relax 6) Feed cats |
+------------------------------------------------------------------------------------+

GROUP_CONCAT和分组结果 假设以下是使用前的结果GROUP_CONCAT

+------------------------+--------------------------+
| ArtistName             | AlbumName                |
+------------------------+--------------------------+
| Iron Maiden            | Powerslave               |
| AC/DC                  | Powerage                 |
| Jim Reeves             | Singing Down the Lane    |
| Devin Townsend         | Ziltoid the Omniscient   |
| Devin Townsend         | Casualties of Cool       |
| Devin Townsend         | Epicloud                 |
| Iron Maiden            | Somewhere in Time        |
| Iron Maiden            | Piece of Mind            |
| Iron Maiden            | Killers                  |
| Iron Maiden            | No Prayer for the Dying  |
| The Script             | No Sound Without Silence |
| Buddy Rich             | Big Swing Face           |
| Michael Learns to Rock | Blue Night               |
| Michael Learns to Rock | Eternity                 |
| Michael Learns to Rock | Scandinavia              |
| Tom Jones              | Long Lost Suitcase       |
| Tom Jones              | Praise and Blame         |
| Tom Jones              | Along Came Jones         |
| Allan Holdsworth       | All Night Wrong          |
| Allan Holdsworth       | The Sixteen Men of Tain  |
+------------------------+--------------------------+
USE Music;
SELECT ar.ArtistName,
    GROUP_CONCAT(al.AlbumName)
FROM Artists ar
INNER JOIN Albums al
ON ar.ArtistId = al.ArtistId
GROUP BY ArtistName;

结果:

+------------------------+----------------------------------------------------------------------------+
| ArtistName             | GROUP_CONCAT(al.AlbumName)                                                 |
+------------------------+----------------------------------------------------------------------------+
| AC/DC                  | Powerage                                                                   |
| Allan Holdsworth       | All Night Wrong,The Sixteen Men of Tain                                    |
| Buddy Rich             | Big Swing Face                                                             |
| Devin Townsend         | Epicloud,Ziltoid the Omniscient,Casualties of Cool                         |
| Iron Maiden            | Somewhere in Time,Piece of Mind,Powerslave,Killers,No Prayer for the Dying |
| Jim Reeves             | Singing Down the Lane                                                      |
| Michael Learns to Rock | Eternity,Scandinavia,Blue Night                                            |
| The Script             | No Sound Without Silence                                                   |
| Tom Jones              | Long Lost Suitcase,Praise and Blame,Along Came Jones                       |
+------------------------+----------------------------------------------------------------------------+

3

就我而言,我必须将一个手机号码唯一的人的所有帐号连接起来。所以我用下面的查询来实现。

SELECT GROUP_CONCAT(AccountsNo) as Accounts FROM `tblaccounts` GROUP BY MobileNumber

查询结果如下:

Accounts
93348001,97530801,93348001,97530801
89663501
62630701
6227895144840002
60070021
60070020
60070019
60070018
60070017
60070016
60070015
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.