简体   繁体   English

如何选择多个喜欢和其他条件的mysql

[英]How to select multiple like and other condition mysql

Good day, I am a newbie programmer. 美好的一天,我是新手程序员。 I would like to ask what is the problem with my code? 我想问一下我的代码有什么问题? All I want sir/ma'am to view the user with letter "a" in firstname,middlename and lastname and status is equal to 1 and usertype is not equal to the word "Admin". 我想让先生/女士查看在名字,中间名和姓氏中字母“ a”且状态等于1且usertype不等于单词“ Admin”的用户。

The code below showing other user record, even the usertype is Admin. 下面的代码显示了其他用户记录,即使用户类型是Admin。 Please help me sir. 先生,请帮我。

This is my code: 这是我的代码:

SELECT * FROM users WHERE usertype != 'Admin' AND firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%' AND userstatus = '1' 

Please help me. 请帮我。 I see some post here that using multiple like, Sorry if my english is too bad. 我在这里看到一些使用多个喜欢的帖子,对不起,如果我的英语不好。 Thanks in adavance 提前感谢

I assume you want to group your LIKE statements. 我假设您想对LIKE语句进行分组。 You can use brackets for that, like so: 您可以为此使用方括号,如下所示:

SELECT * FROM users 
WHERE usertype != 'Admin' 
AND (
    firstname LIKE '%a%' 
    OR middlename LIKE '%a%' 
    OR lastname LIKE '%a%'
)
AND userstatus = '1' 

Or put them the way you want. 或按照您想要的方式放置它们。 However I would recommend you to prevent statements like this, because the can become very ill performing, especially when the wildcharacters are on both sides. 但是,我建议您避免使用这样的语句,因为这样可能会导致性能下降,尤其是当通配符在两侧时。 But I assume that is just as an example here. 但是我认为这只是一个例子。

EDIT : See here for fiddle. 编辑 :参见这里的小提琴。

SELECT * FROM users WHERE usertype != 'Admin' AND (firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%') AND userstatus = '1' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM