是的,只需将当前查询放在内部选择中,然后将新的GROUP BY应用于外部选择.请注意,您可能希望使用GROUP_CONCAT的ORDER BY来确保字符串始终以相同的顺序连接.
SELECT somelist, COUNT(*) FROM
(
SELECT
someid,
GROUP_CONCAT(somestring ORDER BY somestring) AS somelist
FROM table1
GROUP BY someid
) AS T1
GROUP BY somelist
结果:
'Blah,Hello,World', 1
'TestA,TestB,World', 2
这是我使用的测试数据:
CREATE TABLE table1 (someid INT NOT NULL, somestring NVARCHAR(100) NOT NULL);
INSERT INTO table1 (someid, somestring) VALUES
(1, 'Hello'),
(1, 'World'),
(1, 'Blah'),
(2, 'World'),
(2, 'TestA'),
(2, 'TestB'),
(3, 'World'),
(3, 'TestB'),
(3, 'TestA');