如何在mysql中写多个和条件

时间:2017-10-15 20:17:48

标签: mysql sql where-in

受尊敬的开发人员,                       我有以下Mysql查询,它给我错误的结果,查询是这样的

`select * from users
 where (Name='hashaam' or LastName='zahid' or Gender='male') and
 (City='New York' and State='NY') and
 Type=2 and 
 UserId NOT IN (select UserId from profile_detail where 
 UserId=user_profile.UserId)`

有两个'AND'重要条件。那些是

`Type = 2且UserId Not IN ... Rest从输入接收。

WhenEver I执行此查询时,它不仅给我错误的结果,而且还给我重复记录(即使表中没有重复记录)

如何编写多个“和”查询?非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

请求不应返回重复的行:

SELECT * 
FROM 
   users
WHERE  
  (Name='hashaam' OR LastName='zahid' OR Gender='male') 
AND
 City='New York' 
AND 
  State='NY' 
AND
 Type=2 
AND 
 UserId NOT IN (
                SELECT 
                   UserId 
                FROM 
                   profile_detail 
                WHERE 
                   UserId=user_profile.UserId
                 )