mysql 的group_concat函数很好用,可以把列转成行。
SELECT threadid, group_concat(postid separator ',') as postids FROM msg_reply where group by threadid
输出
threadid postids
---------------------
1 1,2,3,4
2 5,6,7
得到postids 后可以很方便做in查询,连implode都省了
完整的语法如下
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])