gpt4 book ai didi

mysql - 如何从具有多个where条件的两个表中获取记录

转载 作者:行者123 更新时间:2023-11-29 04:06:19 25 4
gpt4 key购买 nike

我有两个表,一个是 Userregistration,第二个是 user_verificationcode,我只需要从中获取那些电子邮件和移动状态为 1 的记录。下面是我的表结构

用户注册表

........................................
id fullname mobile_no email
.........................................
5 varun 12344567 abc@gmail
6 nitin 12345678 def@gmail

用户验证码

.............................................
id user_id codetype status
............................................
1 5 email 0
2 5 mobile 1
3 6 email 1
4 6 mobile 1

我想要这样的输出

........................................
id fullname mobile_no email
.........................................
6 nitin 12345678 def@gmail

为此,我使用了下面的查询,但它不起作用,我不知道如何实现这一点。

SELECT * FROM Userregistration 
INNER JOIN user_verificationcode ON Userregistration.`id`=user_verificationcode.`user_id`
where user_verificationcode.`codetype`='email'
and user_verificationcode.`status`='1'
and user_verificationcode.`codetype`='mobile'
and user_verificationcode.`status`='1'

最佳答案

SELECT r.* 
FROM Userregistration r
INNER JOIN user_verificationcode ve ON r.id = ve.user_id
and ve.codetype = 'email'
and ve.status = 1
INNER JOIN user_verificationcode vm ON r.id = vm.user_id
and vm.codetype = 'mobile'
and vm.status = 1

SELECT * 
FROM Userregistration
where id in
(
select user_id
from user_verificationcode
group by user_id
having sum(codetype = 'email' and status = 1) > 0
and sum(codetype = 'mobile' and status = 1) > 0
)

关于mysql - 如何从具有多个where条件的两个表中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37252709/

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