检索fromdate和todate之间的记录,在MYSQL或HQL中传递两个日期和外键值

时间:2015-03-12 06:12:26

标签: mysql hql

检索fromdate和todate之间的记录,传递两个日期和外键值。  enter image description here

我试过这个,

SELECT* FROM Test where T_ID = 1 and '2015-03-10' BETWEEN FROM_DATE and TO_DATE or '2015-03-18' BETWEEN FROM_DATE and TO_DATE;

但它返回外键记录与这些日期之间的另一个外键记录

正确的结果大小应为2。 但这里返回4。

2 个答案:

答案 0 :(得分:2)

查询的第二部分没有给出T_ID。

select * from Test where T_ID = 1 and '2015-03-10' BETWEEN FROM_DATE and TO_DATE or T_ID = 1 and'2015-03-18' BETWEEN FROM_DATE and TO_DATE;

答案 1 :(得分:1)

您需要在OR

中整理()条款
SELECT* FROM Test
 where T_ID = 1 
and ('2015-03-10' BETWEEN FROM_DATE and TO_DATE 
or '2015-03-18' BETWEEN FROM_DATE and TO_DATE);