检查传入日期是在两个单独的列之间

时间:2012-12-10 07:35:55

标签: mysql between

我正在尝试将今天的日期与cur_date()一起使用,并尝试将所有列拉到datestart列和dateend列之间。

我已经尝试了以下两种查询但得到:未知列' tdate'在' where子句'

SELECT id, 
       details, 
       CURDATE() AS tdate 
FROM specials 
WHERE tdate BETWEEN datestart AND dateend;

SELECT id, 
       details, 
       CURDATE() AS tdate 
FROM specials WHERE datestart <= tdate AND dateend >= tdate;

1 个答案:

答案 0 :(得分:2)

在WHERE子句中看不到此字段别名,请尝试此查询 -

SELECT
  id, details, CURDATE() AS tdate FROM specials
WHERE
  datestart <= CURDATE() AND dateend >= CURDATE();

或使用子查询 -

SELECT id, details, tdate FROM (
  SELECT id, details, CURDATE() AS tdate FROM specials
    ) t
WHERE
  datestart <= tdate AND dateend >= tdate;