[英]MySQL SELECT query with OR and Brackets not working
Hi I have a MySQL Select query as follow. 嗨我有一个MySQL Select查询如下。
SELECT *
FROM request
WHERE user_id=$user_id
OR vendor=$user_id
OR (status='' AND services IN ( $service_arr ))
ORDER BY id DESC
But this query is not working as expected. 但是这个查询没有按预期工作。 My OR condition is neglected and this query is getting data only when conditions under brackets are true. 我的OR条件被忽略,只有当括号内的条件为真时,此查询才会获取数据。 Meaning its getting only those rows where column name services
has one of the items present in $service_arr
. 这意味着它只获取列名称services
具有$service_arr
存在的项目之一的那些行。
Thanks 谢谢
This example most probably help you 这个例子很可能对你有帮助
SELECT customer_id,first_name, last_name
FROM customers
WHERE (last_name = 'atiq')
OR (last_name = 'hamza' AND state = 'attock')
OR (last_name = 'usman' AND status = 'Active' AND state = 'islamabad');
I tried creating a SQL fiddle with above table definition, loaded with sample data and tested above SQL. 我尝试用上面的表定义创建一个SQL提琴,加载了示例数据并在SQL之上进行了测试。 it seems to work fine. 它似乎工作正常。
http://sqlfiddle.com/#!9/282e9f/2 http://sqlfiddle.com/#!9/282e9f/2
CREATE TABLE IF NOT EXISTS `request` (
`user_id` int(6) unsigned NOT NULL,
`vendor` int(3) unsigned NOT NULL,
`status` varchar(200) NOT NULL,
`services` varchar(200) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `request` (`user_id`, `vendor`, `status`, `services`) VALUES
('1', '1', 'cut','fit'),
('2', '1', 'cut','fit'), ('3', '3', '','pick'),
('4', '5', 'next','sample');
SELECT *
FROM request
WHERE user_id=1
OR vendor=1
OR (status='' AND services IN ('pick'));
user_id vendor status services
1 1 cut fit
2 1 cut fit
3 3 pick
I could not find anything wrong with your SQL, can you share a sample with the data ? 我发现你的SQL没有任何问题,你能与数据共享样本吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.