1

我有一个类似于下面的查询

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,正如您在第一个查询中看到的那样,我尝试了一些不同的东西。但不能让它产生所需的结果。想法?

4

1 回答 1

2

GROUPin应该提示您-您实际上并没有在查询中对GROUP_CONCAT任何内容进行分组,例如没有GROUP BY子句。由于您没有告诉 MySQL 如何对数据进行分组,因此它没有进行任何分组。因此只能从您的 GROUP_CONCAT 调用中获取一个值。

最有可能你想要的东西更像

SELECT s.ip
etc..
etc..
GROUP BY s.ip  <---this line is missing
于 2013-08-23T21:04:57.890 回答