gpt4 book ai didi

mysql - Mysql 如何从 IN CLAUSE 获取 NULL 值?

转载 作者:行者123 更新时间:2023-11-30 23:47:41 25 4
gpt4 key购买 nike

如果匹配记录存在,我们可以通过IN子句得到结果。
如果记录不存在,是否可以得到NULL值。

例如:

如果 abc@example.comxyz@example.com 存在于表中,而 pqr@example.com 不存在于表中

然后运行这个查询

 SELECT `users`.`email_id`,`users`.`user_id`
FROM `users` WHERE `users`.`email_id`
IN ('abc@example.com','xyz@example.com','pqr@example.com')

结果会是

email_id         | user_id
-----------------+--------
abc@example.com | 1
xyz@example.com | 2

但是否有可能以这种形式获得结果:

email_id        | user_id
----------------+------------
abc@example.com | 1
xyz@example.com | 2
pqr@example.com | NULL --or some value

最佳答案

这是可能的,但不能使用 IN,您可以组合这个返回三个不同行中的电子邮件的查询:

SELECT 'abc@example.com' AS email
UNION ALL SELECT 'xyz@example.com'
UNION ALL SELECT 'pqr@example.com'

对于 users 表,使用 LEFT JOIN:

SELECT e.email, users.user_id
FROM (
SELECT 'abc@example.com' AS email
UNION ALL SELECT 'xyz@example.com'
UNION ALL SELECT 'pqr@example.com'
) e LEFT JOIN users
ON e.email = users.email_id

关于mysql - Mysql 如何从 IN CLAUSE 获取 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645646/

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