聘我网

新概念招聘3.0

MySQL查询

vote up0vote downstar

首先执行如下操作:

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表中得到这个呢?

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

1 个答复

vote up0vote downcheck
SELECT main.id, GROUP_CONCAT(body SEPARATOR ' ') 
FROM main 
LEFT JOIN test1 on main.id=test1.id
GROUP BY main.id
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.