可以将多个行和表的字符串连接到一个结果列中吗? [英] Is it possible to concatenate strings from multiple rows and tables into one result column?

查看:120
本文介绍了可以将多个行和表的字符串连接到一个结果列中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个MySQL查询,从表项目中检索一个与表标签有一对多关系的记录。我的应用程序使用4个表来执行此操作:

 项目 - 项目表
实体 - 实体表;引用几个应用程序资源
标签 - 标签表
Tag_entity - 链接标签到实体

是否可以以这样的方式编写查询,使表标签中的多个值连接到一个结果列中?我不喜欢这样做,而不使用子查询。



表格澄清:

  ------------- 
| Tag_Entity |
------------- ---------- | ----------- | -------
|项目| |实体| | - id | |标签|
| ----------- | | -------- | | - tag_id | | ----- |
| - id | - > | - id | - > | - entity_id | - > | id |
| - entity_id | ---------- ------------- |名称|
------------- -------

期望的结果:

  Projects.id Entities.id Tags.name(concatenated)
1 5'foo ','bar','etc'


解决方案

一个href =http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat =noreferrer> GROUP_CONCAT



示例:

  mysql> SELECT * FROM blah; 
+ ---- + ----- + ----------- +
| K | grp |名称|
+ ---- + ----- + ----------- +
| 1 | 1 | foo |
| 2 | 1 |酒吧|
| 3 | 2 |氢|
| 4 | 4 | dasher |
| 5 | 2 |氦气|
| 6 | 2 |锂|
| 7 | 4 |舞者|
| 8 | 3 | winken |
| 9 | 4 | prancer |
| 10 | 2 |铍|
| 11 | 1 | baz |
| 12 | 3 | blinken |
| 13 | 4 | vixen |
| 14 | 1 | quux |
| 15 | 4 |彗星|
| 16 | 2 |硼|
| 17 | 4 |丘比特|
| 18 | 4 | donner |
| 19 | 4 | blitzen |
| 20 | 3 | nod |
| 21 | 4 | rudolph |
+ ---- + ----- + ----------- +
21行集(0.00秒)

mysql> SELECT grp,GROUP_CONCAT(name ORDER BY K)FROM blah GROUP BY grp;
+ ----- + --------------------------------------- ------------------------- +
| grp | GROUP_CONCAT(名称ORDER BY K)|
+ ----- + --------------------------------------- ------------------------- +
| 1 | foo,bar,baz,quux |
| 2 |氢,氦,锂,铍,硼|
| 3 |眨眼,眨眼,点头
| 4 | dasher,dancer,prancer,vixen,彗星,丘比特,donner,blitzen,rudolph |
+ ----- + --------------------------------------- ------------------------- +
4行集(0.00秒)


I am trying to write a MySQL query that retrieves one record from table "projects" that has a one-to-many relationship with table "tags". My application uses 4 tables to do this:

Projects - the projects table
Entities - entity table; references several application resources
Tags - tags table
Tag_entity - links tags to entities

Is it possible to write the query in such a way that multiple values from table "Tags" are concatenated into one result column? I'd prefer doing this without using subqueries.

Table clarification:

                                      -------------
                                     | Tag_Entity  |
 -------------       ----------      | ----------- |      -------
| Projects    |     | Entities |     | - id        |     | Tags  |
| ----------- |     | -------- |     | - tag_id    |     | ----- |
| - id        | --> | - id     | --> | - entity_id | --> | id    |
| - entity_id |      ----------       -------------      | name  |
 -------------                                            -------

Desired result:

Projects.id   Entities.id   Tags.name (concatenated)
1             5             'foo','bar','etc'

解决方案

see GROUP_CONCAT

example:

mysql> SELECT * FROM blah;
+----+-----+-----------+
| K  | grp | name      |
+----+-----+-----------+
|  1 |   1 | foo       |
|  2 |   1 | bar       |
|  3 |   2 | hydrogen  |
|  4 |   4 | dasher    |
|  5 |   2 | helium    |
|  6 |   2 | lithium   |
|  7 |   4 | dancer    |
|  8 |   3 | winken    |
|  9 |   4 | prancer   |
| 10 |   2 | beryllium |
| 11 |   1 | baz       |
| 12 |   3 | blinken   |
| 13 |   4 | vixen     |
| 14 |   1 | quux      |
| 15 |   4 | comet     |
| 16 |   2 | boron     |
| 17 |   4 | cupid     |
| 18 |   4 | donner    |
| 19 |   4 | blitzen   |
| 20 |   3 | nod       |
| 21 |   4 | rudolph   |
+----+-----+-----------+
21 rows in set (0.00 sec)

mysql> SELECT grp, GROUP_CONCAT(name ORDER BY K) FROM blah GROUP BY grp;
+-----+----------------------------------------------------------------+
| grp | GROUP_CONCAT(name ORDER BY K)                                  |
+-----+----------------------------------------------------------------+
|   1 | foo,bar,baz,quux                                               |
|   2 | hydrogen,helium,lithium,beryllium,boron                        |
|   3 | winken,blinken,nod                                             |
|   4 | dasher,dancer,prancer,vixen,comet,cupid,donner,blitzen,rudolph |
+-----+----------------------------------------------------------------+
4 rows in set (0.00 sec)

这篇关于可以将多个行和表的字符串连接到一个结果列中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆