在BETWEEN子句中使用SQL子查询的日期

时间:2014-06-09 19:33:04

标签: mysql sql

MySQL中的这个多选SQL有什么问题:

select *
from license
where license.expirydate between (
   (select monthrange.monthstart from monthrange where id = 1)
   and
   (select monthrange.monthend from monthrange where id = 1)
)

1 个答案:

答案 0 :(得分:1)

以下是您的查询:

select *
from license
where license.expirydate between ((select monthrange.monthstart from monthrange where id = 1) and 
                                  (select monthrange.monthend from monthrange where id = 1)
                                 )

between的参数不应该在括号中。试试这个:

select *
from license
where license.expirydate between (select monthrange.monthstart from monthrange where id = 1) and 
                                 (select monthrange.monthend from monthrange where id = 1)

当然,您也可以将其表达为join。这个答案仅用于评论语法错误:

select l.*
from license l join
     monthrange m
     on l.expirydate between m.monthstart and m.monthend and
        m.id = 1;