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

时间:2016-05-16 11:20:52

标签: mysql

我有两个表,一个是Userregistration,第二个是user_verificationcode,我必须从中获取那些电子邮件和移动状态为1.Below是我的表结构的记录

用户注册表

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

user_verificationcode

.............................................
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'

4 个答案:

答案 0 :(得分:4)

<WrapContentWebView
            android:layout_width="match_parent"
            android:layout_height="0dp">
</WrapContentWebView>

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 

答案 1 :(得分:1)

您可能只想返回.GreyBG { background: #595959; position: absolute; border-style: solid; width: 50%; //578px; height: 70%; border: 1px solid rgba(0, 0, 0, .2); top: 220px; left: 0px; right: 0px; border-width: 1px; border-color: #21262c; border-radius: 1px; margin-left: auto; margin-right: auto; z-index: -1; }个字段,因为您已经知道<div class="GreyBG"></div> 表中包含的信息。在这种情况下,您可以使用以下查询:

Userregistration

答案 2 :(得分:1)

你可能想做这样的事情

SELECT Userregistration.id, Userregistration.fullName, Userregistration.mobile_no,
Userregistration.email FROM Userregistration
INNER JOIN user_verificationcode
ON Userregistration.id=user_verificationcode.user_id
WHERE user_verificationcode.codetype='email' AND user_verificationcode.status = 1

答案 3 :(得分:0)

SELECT Userregistration.id, 
       Userregistration.fullName, 
       Userregistration.mobile_no, 
       Userregistration.email 
FROM Userregistration 
INNER JOIN user_verificationcode ON      
       Userregistration.id=user_verificationcode.user_id AND    
       user_verificationcode.status=1
WHERE user_verificationcode.codetype='email' AND        
      user_verificationcode.codetype='mobile'