╔════════╦═══════════╦═══════╗
║ MSG_ID ║ RANDOM_ID ║ MSG ║
╠════════╬═══════════╬═══════╣
║ 1 ║ 22 ║ apple ║
║ 2 ║ 22 ║ bag ║
║ 3 ║ 0 ║ cat ║
║ 4 ║ 0 ║ dog ║
║ 5 ║ 0 ║ egg ║
║ 6 ║ 21 ║ fish ║
║ 7 ║ 21 ║ hen ║
║ 8 ║ 20 ║ glass ║
╚════════╩═══════════╩═══════╝
结果要求:
╔═══════════╦═══════════╗
║ RANDOM_ID ║ MSG ║
╠═══════════╬═══════════╣
║ 22 ║ apple bag ║
║ 0 ║ cat ║
║ 0 ║ dog ║
║ 0 ║ egg ║
║ 21 ║ fish hen ║
║ 20 ║ glass ║
╚═══════════╩═══════════╝
我希望它按random_id分组但是对于random_Id = 0我不希望它被分组。
任何建议都将得到赞赏。
Select random_id as a,
(
Select group_concat( message ORDER BY random_id ASC SEPARATOR ' ')
from messagemaster
where flag = 0 AND random_id=a
group by random_id
order by random_id
) as msg
from messagemaster
where flag = 0
答案 0 :(得分:1)
SELECT Random_ID, GROUP_CONCAT(msg SEPARATOR ' ') Msg
FROM TableName
WHERE Random_ID <> 0
GROUP BY Random_ID
UNION
SELECT Random_ID, Msg
FROM TableName
WHERE Random_ID = 0