我有一个SQL查询
SELECT e.id,es.userfullname created_by_user,es1.userfullname assigned_to_user
FROM lead_activity h
JOIN employees es1 on h.assign_to = es1.user_id
JOIN leads e ON h.lead_id = e.id
JOIN employees es on e.created_by = es.user_id
WHERE e.isactive = 1 and DATE_FORMAT(e.date_created,'%Y-%m-%d') = '2018-12-05'
ORDER BY e.created DESC,h.id ASC
其输出如下所示
------------------------
|id | created | assign |
|----------------------|
| 1 | Arijit | Ashish1 |
| 1 | Arijit | Amit2 |
| 1 | Arijit | Vishal3 |
| 1 | Arijit | Vikas4 |
| 2 | Vinod | Aditya1 |
| 2 | Vinod | Gourav2 |
| 2 | Vinod | Alok3 |
------------------------
但我想要下表所示的结果。
------------------------------------------------------------
| id | created | assign 1 | assign 2 | assign 3 | assign 4 |
------------------------------------------------------------
| 1 | Arijit | Ashish1 | Amit2 | Vishal3 | Vikas4 |
| 2 | Vinod | Aditya1 | Gourav2 | Alok3 | Null |
------------------------------------------------------------
我在选择中使用GROUP concatenate (GROUP_CONCAT)
函数尝试了Group By,但是问题是组连接起来改变了值的顺序,同时用逗号分隔列值,而我的主要目标是明智地显示数据级别。因此,我不能使用组级联。建议一些事情,以便组级联功能不更改顺序或在上面的查询中更改,以便我提前获得以上结果。
答案 0 :(得分:0)
GROUP_CONCAT(assign ORDER BY assign SEPARATOR ' ')
使多个列跟在 'pivot' 标签之后。