我有一个像this这样的表,其中包含团队ID和消息ID。每次运行查询时,我都必须为每个团队获得唯一 msg_id。
示例:
ID TEAM_ID MSG_ID
1 1 10
2 1 11
3 2 12
4 2 13
5 2 14
有效输出:
1
TEAM_ID MSG_ID
1 11
2 13
2
TEAM_ID MSG_ID
1 10
2 12
答案 0 :(得分:0)
如果您想为每个TEAM_ID返回一个随机相关的MSG_ID:
SELECT t.TEAM_ID,
(SELECT MSG_ID
FROM team_message
WHERE TEAM_ID = t.TEAM_ID
ORDER BY RAND()
LIMIT 1) MSG_ID
FROM (SELECT DISTINCT TEAM_ID
FROM team_message) t;
答案 1 :(得分:0)
SELECT * FROM (SELECT * FROM team_message ORDER BY rand()) as t GROUP BY TEAM_ID;