如何在此查询中添加DESC?

时间:2017-11-29 05:21:09

标签: mysql sql mysqli

我正在做一个项目而且我正在使用MYSQLI我只需要让它下降。我怎么能这样做,因为我这样做的常用方式不起作用。

SELECT * FROM posts 
LEFT JOIN members ON posts.user_id = members.user_id 
UNION 
SELECT * FROM posts 
RIGHT JOIN members ON posts.user_id = members.user_id 
WHERE posts.user_id IS NOT NULL

这就是我试过的

SELECT * FROM posts LEFT JOIN members ON posts.user_id = members.user_id UNION SELECT * FROM posts RIGHT JOIN members ON posts.user_id = members.user_id WHERE posts.user_id IS NOT NULL AND ORDER BY posts.user_id DESC

1 个答案:

答案 0 :(得分:0)

我建议您使用完全外连接,而不是分别使用左右连接。并且还删除在ORDER之前放置的AND。

这样您的查询就像

SELECT * FROM POSTS FULL OUTER JOIN MEMBERS ON POSTS.user_id = MEMBERS.user_id 
WHERE POSTS.user_id  IS NOT NULL 
ORDER BY POSTS.user_id DESC;