我正在使用MySQL,目前我的查询返回的内容如下:
|ID|Tags|
|01|tag1,tag2,tag2,tag2
|02|tag2,tag2
|03|tag2,tag2,tag2,tag2,tag3
我希望它能够回归:
|ID|Tags|
|01|tag1,tag2(x3)
|02|tag2(x2)
|03|tag2(x4),tag3
预期重复的值。我想要的只是将它们分组。它甚至可能吗?
我正在使用GROUP_CONCAT(table.column)
,它会将结果返回给我。
答案 0 :(得分:1)
SELECT id, GROUP_CONCAT(final_tags)
FROM(
SELECT *
FROM(
SELECT
tag,
combined,
IF(tag <> combined, CONCAT(tag, combined), tag) AS final_tags,
counted,
id
FROM (
SELECT
id,
tag,
IF(@A = tag, @B := @COUNTER, @B := 1) AS counted,
IF(@A = tag, @D := CONCAT('(x', @COUNTER, ')'), @D := tag) AS combined,
@A := tag,
IF(@B = 1, @COUNTER := 1, @COUNTER),
@COUNTER := @COUNTER + 1
FROM myTable
JOIN (SELECT @A := '', @D := '', @COUNTER := 1, @B := 0) AS t
) AS temp
ORDER BY counted DESC
) AS whatever
GROUP BY tag
) AS this_is_it
GROUP BY id
SEE FIDDLE需要澄清