gpt4 book ai didi

MySQL:如果存在于其他表中,则按列排序

转载 作者:行者123 更新时间:2023-11-29 01:39:45 25 4
gpt4 key购买 nike

我有两个表

  1. 用户(用户 ID、姓名、位置、类型等)
  2. 在线用户(用户 ID、状态等)

我想列出所有 users where type = 'sometype' 并以 asc 顺序排列这些 usersfirst 所有用户都存在于 onlineusers 表中,然后是那些不在 onlineusers 表中的用户。

我已经设法首先获得所有在线用户,但他们没有按升序排列。

这是我尝试过的

SELECT * FROM users u 
LEFT JOIN onlineusers o on u.userID = o.userID
where u.type = 'sometype'
order by IFNULL(o.userID,9999) desc;

谢谢

最佳答案

SELECT * 
FROM users u
LEFT JOIN onlineusers o on u.userID = o.userID
where u.type = 'sometype'
order by o.userID is null,
u.userID

o.userID is null 为不在线的用户返回 1 (true),如果在线则返回 0 (false)。您可以按这 2 个值排序。

关于MySQL:如果存在于其他表中,则按列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046396/

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