本文介绍了mysql多个地方和地方不在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目前看起来像这样的mysql查询,

I have a mysql query that currently looks like this,

select * from `users`
where `first_name` LIKE "%Martin%"
or `last_name` LIKE "%Martin%"
or `email` LIKE "%Martin%"
and `users`.`id` not in
(186,85,184,252,256,181)

我的表中的用户的first_name属性为 Martin 并且其ID为 181 ,但数据库中已返回该值,我怎么会认为我的mysql有一些混乱呢?

I have user in my table with the first_name attribute being Martin and their ID being 181 yet there are being returned in the database, how come I am assuming there is some confusion in my mysql?

我想返回名,姓或电子邮件像字符串一样的所有用户,除非用户id与数组的id相匹配?

I wanting to return all users where the first_name, last_name or email are LIKE a string unless the users id matches an id an array?

我错了吗?

推荐答案

您必须使用括号:

select *
from `users`
where (`first_name` LIKE "%Martin%" or
       `last_name` LIKE "%Martin%" or
       `email` LIKE "%Martin%" ) and
       `users`.`id` not in (186,85,184,252,256,181)

AND 的优先级高于 OR ,因此,上面没有括号的查询 无法正常工作

AND takes precedence over OR, hence the above query without the parentheses doesn't work as intended.

这篇关于mysql多个地方和地方不在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:40