MYSQL连接查询不显示特定条件不起作用?

时间:2014-05-01 18:05:01

标签: php mysql

我有以下查询,我试图在搜索first_name或last_name或电子邮件时使用此查询,但不要包括任何role_id<> 0:这是查询:

select *, `users`.`id` as `user_id` 
from `users` 
inner join `roles` on `users`.`role_id` = `roles`.`id` 
where `users`.`first_name` LIKE %test% 
   or `users`.`last_name` LIKE %test% 
   or `users`.`email` LIKE %test% 
   and `users`.`role_id` <> 0 
order by `first_name` asc limit 2 offset 0

这不能正常显示用户的role_id = 0?你能解释一下原因吗?以及如何解决它

由于

1 个答案:

答案 0 :(得分:1)

select *, `users`.`id` as `user_id` 
from `users` 
inner join `roles` on `users`.`role_id` = `roles`.`id` 
where (`users`.`first_name` LIKE %test% 
   or `users`.`last_name` LIKE %test% 
   or `users`.`email` LIKE %test% )
   and `users`.`role_id` <> 0 
order by `first_name` asc limit 2 offset 0

括号帮助区分逻辑与什么。没有你所说的括号:

名字匹配,或第二名称匹配或(电子邮件匹配和角色匹配)