gpt4 book ai didi

php - MySQL:如何检查是否列出了用户

转载 作者:行者123 更新时间:2023-11-30 22:37:49 26 4
gpt4 key购买 nike

我想知道如何检查用户是否已经在数据库中。

在 PHP 中,我有一个包含一些 UserID 的数组。即 userIDs[0] = 1234; userIDs[1] = 2345;

现在我想构建一个查询,如果可能的话只进行一个 sql 调用以获得以下结果:

############################
# UserID # Exists #
############################
# 1234 # 0 #
# 2345 # 1 #
############################

是否有 sql 解决方案,或者我是否必须通过单独的调用来检查每个 ID?感谢您的帮助!

最佳答案

根据列表检查现有的 id

select userId, userId in (2345,5678) as `exists`
from your_table
order by userId

但是要检查 id 的列表是否在数据库中,您可以这样做

select tmp.userId, tab.userId is not null as `exists`
from
(
select 1234 as userId
union all
select 2345
union all
select 3456
) tmp
left join db_table tab on tab.userId = tmp.userId

关于php - MySQL:如何检查是否列出了用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971214/

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