使用OR和AND问题进行MYSQL查询 [英] MYSQL Querying using OR and AND problem

查看:91
本文介绍了使用OR和AND问题进行MYSQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在mysql查询中遇到问题,我正在为我的搜索查询
客户来检索客户信息,但是我在查询中使用OR和AND时遇到了麻烦,当我输入OR时(例如OR ORLIKE LIKE''%search_customer%''),我的查询有效,但查询会检索所有信息而无视我的AND语句,例如
(user_status =活动"),但是当我删除OR时,我的查询效果很好,
但是我需要OR语句来搜索表中的2个以上字段,并同时选择所有user_status ="active",我的查询有问题吗?请帮助我,因为我确实需要修复此查询,原因是在某些模块中需要这种数据检索,非常感谢

  SELECT  *
 FROM  tblusers
位置
business_name 喜欢 ' %search_customer%'  OR  emal  '  AND  user_status = '  active';
订单 依据 business_name  ASC  

解决方案

您必须将SQL语句重写为

  SELECT  *
 FROM  tblusers
位置
(business_name 喜欢 "   OR  emal 喜欢 " %search_customer%")
 AND  user_status = " 
订单 依据 business_name  ASC  



在business_name和emal之间缺少使用("和)".

您的WHERE声明如下:

business_name OR emal OR user_status ="active" emal .应该是电子邮件,只是您问题中的错字,而不是实际的SQL中的错字).我建议您在查询中包括字段名称,而不要使用星号选择所有字段,但这仅是我自己.


感谢Kim Togo6 :)我已经测试了新代码和它的工作,以为我只是想念一个
查询中的()",再次谢谢您,您保存了我的一天!


Helo, im just having a problem in mysql query, i am making a search query for my
customer to retrieve customer information, but im having trouble in using the OR and AND in my query, when i put an OR e.g(OR email LIKE ''%search_customer%'') my query works but the query retrieve all the information and disregard my AND statement e.g
(user_status = ''active'') but when i remove the OR my query works pretty well,
but i need the OR statement to search more than 2 fields in my table and select all the user_status = "active" as well, is there something wrong with my query? Please help me cause i really need this query to be fixed cause i will be needing this kind of data retrieving in some of my module, Thank you very much

SELECT *
FROM tblusers
WHERE
business_name LIKE '%search_customer%' OR emal LIKE '%search_customer%'
AND user_status = 'active';
ORDER by business_name ASC

解决方案

You have to rewrite the SQL statement to

SELECT *
FROM tblusers
WHERE
(business_name LIKE "%search_customer%" OR emal LIKE "%search_customer%")
AND user_status = "active"
ORDER by business_name ASC



Use are missing "(" and ")" between business_name and emal.

Your WHERE statement is like:

business_name OR emal OR user_status = "active"


Well, I tried your SQL and, other than the semi-colon in the wrong place, it works fine (I take it that emal. is meant to be email and is just a typo in your question and not in the actual SQL). I would advise you to include the field names in your query rather than using the asterisk to select all fields, but that is just me.


Thanks Kim Togo6 :) i have test the new code and its work, to think that i am just missing a
"( )" in the query, thank you again, you saved my day!


这篇关于使用OR和AND问题进行MYSQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆