改进下面的mysql查询

时间:2015-08-06 10:26:58

标签: mysql

SELECT * FROM ***** 
WHERE ((first_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28') 
      or (last_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28'))

在上面的查询中,我不想一次又一次地输入日期(2015-07-28)我可以使用变量并为该变量赋值吗

day ='2015-07-28';

SELECT * FROM ***** 
WHERE ((first_appeared_on BETWEEN DATE_SUB(day , INTERVAL 1 DAY ) AND day) 
      or (last_appeared_on BETWEEN DATE_SUB( day , INTERVAL 1 DAY ) AND day))

1 个答案:

答案 0 :(得分:0)

这可以在MySQL中使用user-defined variable

完成
SET @day = '2015-07-28';

SELECT * FROM ***** 
WHERE ((first_appeared_on BETWEEN DATE_SUB( @day , INTERVAL 1 DAY ) AND @day) 
  or (last_appeared_on BETWEEN DATE_SUB( @day , INTERVAL 1 DAY ) AND @day));