我有一个类似于下面的查询
SELECT s.ip,
t.name AS 'typeName',
e.name AS 'envName',
i.name,
s.computerName
#ap.name
#GROUP_CONCAT(DISTINCT i.name ORDER BY i.name ASC SEPARATOR ', ' ) AS instances
#GROUP_CONCAT(ap.name ORDER BY ap.name ASC SEPARATOR ', ' ) AS AppPool
FROM servers AS s
JOIN types AS t ON s.typeID = t.id
JOIN environments AS e ON s.envID = e.id
JOIN serverinstances AS si ON s.id = si.serverID
JOIN instances AS i ON si.instanceID = i.id
JOIN serverapppool AS sa ON s.id = sa.serverID
JOIN apppools AS ap ON sa.appPoolID = ap.id
WHERE computerName IS NOT NULL
上述查询的结果
| ip | type | env | instance | appPool | compName |
|_____________|______|_____|__________|_________|__________|
| 192.168.1.1 | App | test| com1 | fo | name1 |
| 192.168.1.1 | App | test| com1 | bo | name1 |
| 192.168.1.1 | App | test| com2 | fo | name1 |
| 192.168.1.1 | App | test| com2 | bo | name1 |
| 192.168.1.2 | App | test| com1 | fo | name2 |
| 192.168.1.2 | App | test| com1 | bo | name2 |
| 192.168.1.2 | App | test| com2 | fo | name2 |
| 192.168.1.2 | App | test| com2 | bo | name2 |
| 192.168.1.3 | App | prod| com1 | fo | name3 |
| 192.168.1.4 | App | prod| com1 | fo | name4 |
| 192.168.1.5 | App | prod| com1 | bo | name5 |
这些结果是准确的。但是我需要浓缩它们。目标是使它们看起来像下面
| ip | type | env | instances | appPools | compName |
|_____________|______|_____|_____________|__________|__________|
| 192.168.1.1 | App | test| com1, com2 | fo, bo | name1 |
| 192.168.1.2 | App | test| com1, com2 | fo, bo | name2 |
| 192.168.1.3 | App | prod| com1 | fo | name3 |
| 192.168.1.4 | App | prod| com1 | fo | name4 |
| 192.168.1.5 | App | prod| com1 | bo | name5 |
我相信这可以通过使用来完成GROUP_CONCAT
,正如您在第一个查询中看到的那样,我尝试了一些不同的东西。但不能让它产生所需的结果。想法?