sqlite 行转列 作者:马育民 • 2020-05-16 21:33 • 阅读:10391 # 案例 class表 [![](https://www.malaoshi.top/upload/0/0/1EF5XCNHtEwV.png)](https://www.malaoshi.top/upload/0/0/1EF5XCNHtEwV.png) student表 [![](https://www.malaoshi.top/upload/0/0/1EF5XCNdsWcY.png)](https://www.malaoshi.top/upload/0/0/1EF5XCNdsWcY.png) 执行关联查询: ``` select a.cname,b.sname from class a,student b where a.cid=b.cls_id order by a.cname,b.sname ``` 执行结果: [![](https://www.malaoshi.top/upload/0/0/1EF5XCOJXzkb.png)](https://www.malaoshi.top/upload/0/0/1EF5XCOJXzkb.png) 但现在只想出现2个班级,学生姓名在一行显示,代码如下: ``` select a.cname,group_concat(b.sname) from class a,student b where a.cid=b.cls_id group by a.cname order by a.cname,b.sname ``` 执行结果: [![](https://www.malaoshi.top/upload/0/0/1EF5XCQ59igf.png)](https://www.malaoshi.top/upload/0/0/1EF5XCQ59igf.png) # group_concat ``` group_concat( distinct,separator ); ``` - distinct:要连接的字段 - separator:分隔符 ### 例子 设置分隔符 ``` select a.cname,group_concat(b.sname, '、') as 学生 from class a,student b where a.cid=b.cls_id group by a.cname order by a.cname,b.sname ``` 感谢: http://blog.csdn.net/sinat_27406925/article/details/77507478 原文出处:http://malaoshi.top/show_1EF5XCggAWu0.html