带有OR和括号的MySQL SELECT查询不起作用

时间:2019-03-17 02:13:48

标签: php mysql database

嗨,我有一个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

但是此查询无法正常工作。我的OR条件被忽略,仅当括号下的条件为true时,此查询才获取数据。 这意味着它仅获取那些列名services具有$service_arr中存在的项目之一的行。

谢谢

2 个答案:

答案 0 :(得分:0)

此示例最有可能帮助您

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');

答案 1 :(得分:0)

我尝试用上面的表定义创建一个SQL提琴,加载了示例数据并在SQL上进行了测试。它似乎工作正常。

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

我的SQL找不到任何问题,可以与数据共享样本吗?