查询之间的MySql日期出错了

时间:2011-09-17 04:12:18

标签: php mysql

select si.total 
from sales_inv si,c nf_order co 
where si.order_num=co.order_no 
and co.cnf_no='CNF001' 
and co.added_on 
between '$month1' and '$month2'

在此查询中,我想从表sales_inv中获取销售发票中的order_numorder_number中的cnf_order应该相等的总数。

added_on是一个时间戳,应该在$month1$month2之间

$month1$month2是以时间戳格式存储数月的变量

这个查询不起作用,我做错了什么?

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

select si.total 
from sales_inv si,c nf_order co 
where si.order_num=co.order_no 
and co.cnf_no='CNF001' 
and ( co.added_on 
between '$month1' and '$month2')

编辑: -

使用日期功能

   select si.total 
    from sales_inv si,c nf_order co 
    where si.order_num=co.order_no 
    and co.cnf_no='CNF001' 
    and ( DATE(co.added_on) 
    between DATE('$month1') and DATE('$month2'))

答案 1 :(得分:0)

尝试:

select si.total 
from sales_inv si,c nf_order co 
where si.order_num=co.order_no 
and co.cnf_no='CNF001' 
and co.added_on 
between STR_TO_DATE('$month1', '%Y-%m-%d') and STR_TO_DATE('$month2', '%Y-%m-%d')