gpt4 book ai didi

mysql - 如何使用 left join 和 order by?

转载 作者:太空宇宙 更新时间:2023-11-03 11:05:59 26 4
gpt4 key购买 nike

我有 USER 表(主表)和 ONLINE 表(与 USER 具有一对一关系的引用表),如下所示:我在表上使用左连接 enter image description here

我正在在线表的“is_logged”字段上应用订单。(ASC)我按顺序获得了记录,但所有记录都排在第一位,而这些记录不在 ONLINE 表中。我想要所有用户表顺序的记录,但那些记录应该排在最后,在线表中不存在

最佳答案

检查一下。

SELECT  o.*
FROM users u
LEFT JOIN online
o ON u.id = o.user_id
ORDER BY COALESCE(is_logged, -1) DESC, user_id ASC

SQLFiddle Demo

关于mysql - 如何使用 left join 和 order by?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11876335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com