╔════════╦═══════════╦═══════╗
║ 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