sql - MySQL查询: How to get body concatenation from test1 with space as joint,得到 "something1 something2"?

标签 sql mysql

设置:

mysql> create table main(id integer unsigned);

mysql> create table test1(id integer unsigned,body text);
Query OK, 0 rows affected (0.84 sec)

mysql> insert into main(id) value(1);
Query OK, 1 row affected (0.16 sec)

mysql> insert into test1(id,body) value(1,'something1');
Query OK, 1 row affected (0.27 sec)

mysql> insert into test1(id,body) value(1,'something2');
Query OK, 1 row affected (0.00 sec)

使用

mysql> select main.id,body from main
    -> left join test1 on main.id=test1.id
    -> group by main.id;

返回:

+------+------------+
| id   | body       |
+------+------------+
|    1 | something1 |
+------+------------+
1 row in set (0.02 sec)

如何从 test1 中获取以空间为关节的主体连接,以获得“something1 something2”?

最佳答案

SELECT main.id, GROUP_CONCAT(body SEPARATOR ' ') 
FROM main 
LEFT JOIN test1 on main.id=test1.id
GROUP BY main.id

参见 documentation了解详情。

关于sql - MySQL查询: How to get body concatenation from test1 with space as joint,得到 "something1 something2"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1499336/

相关文章:

mysql - 不能从一个值中减去另一个值(无符号)

php - PDO 在提交/回滚之前丢失事务

php CRUD 传递参数

python - Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝)

mysql - 如何在存储过程中使用类似语法

mysql - 如何以编程方式将新用户插入我的论坛?

c# - SQL 服务器 :A network-related or instance-specific error occurred

sql - 为 int8 获取 postgresql 中可用的最大免费 ID

mysql - 在 MYSQL 中按英寸值排序

PHP 使用密码加密方案的安全登录系统