我的桌子就是这样。
Location | Building | Department |
L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3
我想要做的是一个查询,该查询按建筑物计算位置和组,然后以字符串形式显示该建筑物中的部门
Building | Count L | Departments |
B1 | 2 | D1,D3
B2 | 1 | D2
答案 0 :(得分:2)
您可以使用Group_Concat
功能
定义:
此函数返回带有连接的非NULL的字符串结果 组中的值。如果没有非NULL值,则返回NULL
查询如下:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
答案 1 :(得分:0)
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
这也将有助于解决空问题。