gpt4 book ai didi

mysql - 使用 "IN"语句设置默认值?

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

如何使用“IN”语句设置默认值,如下所示:

SELECT username
FROM user
WHERE id IN (1,2,3,4)
ORDER BY FIELD(id,1,2,3,4)

如果 id = 2 不可用则返回 0null,如下所示:

John33
0
amanda1
erik

最佳答案

您需要使用 left join 执行此操作:

SELECT u.username
FROM (select 1 as id union all select 2 union all select 3 union all select 4
) ids left join
user u
on u.id = ids.id
ORDER BY FIELD(id, 1, 2, 3, 4);

where 子句中的逻辑不能向结果集“添加”新行。

关于mysql - 使用 "IN"语句设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26077289/

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