mysql - UNION的唯一记录

时间:2015-01-15 11:03:31

标签: mysql group-by unique union

在论坛中,以下查询应从2个随机和唯一线程(由parent_id表示)中的每一个中提取2个帖子。我如何确保parent_id确实是唯一的?

(SELECT parent_id, post_content, nickname FROM posts AS a JOIN users ON users.user_id=a.user_id JOIN

(SELECT parent_id AS c FROM posts LIMIT 1) AS b ON a.parent_id=b.c ORDER BY post_id DESC LIMIT 2)

UNION ALL

(SELECT parent_id, post_content, nickname FROM posts AS a JOIN users ON users.user_id=a.user_id JOIN

(SELECT parent_id AS e FROM posts LIMIT 1) AS d ON a.parent_id=d.e ORDER by post_id DESC LIMIT 2)

ORDER BY RAND()

我尝试添加group by和union distinct但是我一直在使用相同的parent_id获取记录..(除非我将组放在错误的位置)。

0 个答案:

没有答案