查询未返回预期结果

时间:2017-03-04 02:34:19

标签: mysql sql

我有这个问题:

 SELECT * FROM Sales 
 WHERE (Date BETWEEN '2017-01-01' AND '2017-02-28') 
   AND (ID="Search" OR Product="Search");

它应该仅返回那两个ID os产品等于搜索的日期之间的销售,但它返回这些日期之间的所有结果,即使ID或产品都没有=搜索,我也不明白为什么

修改

样本数据

ID      Date          Product
1       2017-01-11    Product A
2       2017-02-01    Product C
3       2017-02-05    Product B
4       2017-03-01    Product A

如果我的查询是

SELECT * FROM Sales 
WHERE (Date BETWEEN '2017-01-01' AND '2017-02-28') 
 AND (ID="Product C" OR Product="Product C");

我在期待

ID      Date          Product  
2       2017-02-01    Product C  

但它返回

ID      Date          Product  
1       2017-01-11    Product A  
2       2017-02-01    Product C  
3       2017-02-05    Product B  

提前致谢

1 个答案:

答案 0 :(得分:1)

它没问题。例如,

我的桌子

+----+------+---------------------+ | id | name | create_at | +----+------+---------------------+ | 1 | 1 | 2017-03-04 11:06:25 | | 2 | 2 | 2017-03-04 11:09:27 | | 3 | 4 | 2017-03-04 11:06:56 | +----+------+---------------------+

我的sql

SELECT * FROM test WHERE (create_at BETWEEN '2017-03-04 11:06:00' AND '2017-03-04 11:10:00') AND (id="2" OR name="2");

我的结果

+----+------+---------------------+ | id | name | create_at | +----+------+---------------------+ | 2 | 2 | 2017-03-04 11:09:27 | +----+------+---------------------+