如何在mysql中连接名称

时间:2018-02-01 08:07:07

标签: mysql

我有疑问:

SELECT YEAR(t1.changedtime) as year, MONTHNAME(t1.changedtime) as months, t6.pname FROM `working_scene` t1 
LEFT JOIN( SELECT longname, username, user_type, email, uid, staff_id, access, fkdeptid, fkhouseid, fkhouseroleid FROM `users` )t2 ON t2.uid = t1.owner 
LEFT JOIN( SELECT longname as squad_leader, uid FROM `users` )t3 ON t3.uid = t1.fkslid 
LEFT JOIN( SELECT weid, fkwpid FROM `working_episode` )t4 ON t4.weid = t1.fkweid 
LEFT JOIN( SELECT wpid, fkpid, fkrftid FROM `working_project` )t5 ON t5.wpid = t4.fkwpid 
LEFT JOIN( SELECT pid, pname FROM `master_project` )t6 ON t6.pid = t5.fkpid WHERE t2.staff_id = '1000368' GROUP BY t6.pname, months ORDER BY changedtime ASC

哪会给我结果:

enter image description here

如何在一个月内删除月份复制并打印项目名称昏迷?

例如:

| year |  months  |  pname   |
+------+----------+----------+
| 2016 | November | Hanazuki |
| 2016 | November | Niko     |

成为:

| year |  months  |     pname      |
+------+----------+----------------+
| 2016 | November | Hanazuki, Niko |

修改

我试过了group_concat,但它没有按我想要的方式工作。这是什么

SELECT YEAR(t1.changedtime) as year, 
       MONTHNAME(t1.changedtime) as months,
       GROUP_CONCAT(t6.pname)
FROM `working_scene` t1 
      LEFT JOIN( SELECT longname, username, user_type, email, uid, staff_id, access, fkdeptid, fkhouseid, fkhouseroleid FROM `users` )t2 ON t2.uid = t1.owner 
      LEFT JOIN( SELECT longname as squad_leader, uid FROM `users` )t3 ON t3.uid = t1.fkslid 
      LEFT JOIN( SELECT weid, fkwpid FROM `working_episode` )t4 ON t4.weid = t1.fkweid 
      LEFT JOIN( SELECT wpid, fkpid, fkrftid FROM `working_project` )t5 ON t5.wpid = t4.fkwpid 
      LEFT JOIN( SELECT pid, pname FROM `master_project` )t6 ON t6.pid = t5.fkpid 
WHERE t2.staff_id = '1000368' 
GROUP BY YEAR(t1.changedtime), MONTHNAME(t1.changedtime)
ORDER BY changedtime ASC

enter image description here

1 个答案:

答案 0 :(得分:1)

MySQL有一个名为GROUP_CONCAT的内置聚合函数。

home