Group concat 包含相应的数据
Group concat to include corresponding data
这是我的 table 结构:
orders
--------------------------
id | customer_name
--------------------------
23 | John Doe
24 | Jane Doe
order_comments
--------------------------------------------------------------------
id | order_id | username | created_at | comment
--------------------------------------------------------------------
1 | 23 | Bob | 2019-04-01 | my first comment
2 | 23 | Jim | 2019-04-03 | another comment
3 | 24 | Jim | 2019-04-05 | testing
4 | 24 | Jim | 2019-04-06 | testing again
我想要 select 由换行符连接的评论,但还包括用户名和 created_at。这是我目前所拥有的:
select *
from (SELECT order_id, GROUP_CONCAT(`comment` order by id desc SEPARATOR '\n') as comments
FROM `order_comments`
group by order_id) comments
结果:
order_id | comments
--------------------------------
23 | my first comment
| another comment
--------------------------------
24 | testing
| testing again
这是我想要的结果,其中包含用户名并在每个评论连接中创建:
order_id | comments
--------------------------------
23 | Bob on 2019-04-01:
| my first comment
|
| Jim on 2019-04-03:
| another comment
---------------------------------
24 | Jim on 2019-04-05:
| testing
|
| Jim on 2019-04-06:
| testing again
怎样才能得到想要的结果?
GROUP_CONCAT
或简单地 CONCAT
:
SELECT order_id,CONCAT(username,' on ',created_at,' ',comments)
FROM order_comments
ORDER BY order_id;
另一种方法是 CONCAT_WS
> 使用分隔符连接:
SELECT order_id,CONCAT_WS(' ',username,'on',created_at,comments)
FROM order_comments
ORDER BY order_id;
试试这个
select *
from (SELECT order_id, GROUP_CONCAT(
DISTINCT CONCAT(username,’ on ‘, created_at, ‘:’, comment order by id desc SEPARATOR '\n') as comments
FROM `order_comments`
group by order_id) comments
这是我的 table 结构:
orders
--------------------------
id | customer_name
--------------------------
23 | John Doe
24 | Jane Doe
order_comments
--------------------------------------------------------------------
id | order_id | username | created_at | comment
--------------------------------------------------------------------
1 | 23 | Bob | 2019-04-01 | my first comment
2 | 23 | Jim | 2019-04-03 | another comment
3 | 24 | Jim | 2019-04-05 | testing
4 | 24 | Jim | 2019-04-06 | testing again
我想要 select 由换行符连接的评论,但还包括用户名和 created_at。这是我目前所拥有的:
select *
from (SELECT order_id, GROUP_CONCAT(`comment` order by id desc SEPARATOR '\n') as comments
FROM `order_comments`
group by order_id) comments
结果:
order_id | comments
--------------------------------
23 | my first comment
| another comment
--------------------------------
24 | testing
| testing again
这是我想要的结果,其中包含用户名并在每个评论连接中创建:
order_id | comments
--------------------------------
23 | Bob on 2019-04-01:
| my first comment
|
| Jim on 2019-04-03:
| another comment
---------------------------------
24 | Jim on 2019-04-05:
| testing
|
| Jim on 2019-04-06:
| testing again
怎样才能得到想要的结果?
GROUP_CONCAT
或简单地 CONCAT
:
SELECT order_id,CONCAT(username,' on ',created_at,' ',comments)
FROM order_comments
ORDER BY order_id;
另一种方法是 CONCAT_WS
> 使用分隔符连接:
SELECT order_id,CONCAT_WS(' ',username,'on',created_at,comments)
FROM order_comments
ORDER BY order_id;
试试这个
select *
from (SELECT order_id, GROUP_CONCAT(
DISTINCT CONCAT(username,’ on ‘, created_at, ‘:’, comment order by id desc SEPARATOR '\n') as comments
FROM `order_comments`
group by order_id) comments