查询同一张表符合条件的某些数据的id拼接成一个字段返回

同一张表存在类似多级菜单的上下级关系的数据,查询出符合条件的某些数据的id拼接成一个字段返回:

SELECT CONCAT(a.pid, ',', b.subid) AS studentIDS FROM 
(SELECT id as pid, sourceCode as scode FROM student WHERE studentType='父级条件1' AND studentCode='父级条件2') a 
LEFT JOIN 
(SELECT id as subid, parentCode pcode FROM student WHERE studentType='子级条件1' AND parentCode='父级条件2' 
AND sourceCode='子级条件2'
) b
ON a.scode=b.pcode;

 

posted @ 2019-05-16 13:41  wmqiang  阅读(443)  评论(0编辑  收藏  举报