比较日期mysql

时间:2013-01-06 15:54:24

标签: mysql string-comparison date-comparison

我的SQL查询存在问题。

在数据库中我有一张发票的到期日。

保存为字符串12-01-2013

现在我想将此日期与今天进行比较。但是没有用。

这是代码。

include_once( '配置/ connect.php');

connectdb();

$name = $_GET['name'];

$date = date('d-m-Y');

$qry = "SELECT *, finished_invoice.date AS invoice_date FROM finished_invoice
    INNER JOIN customer 
    ON finished_invoice.customer = customer.customer_nr 
        WHERE finished_invoice.user = '".$_SESSION['id']."' 
    AND customer.company_name = '$name'
    AND expiry_date < '$date'
    AND finished_invoice.sent > '0'
    AND finished_invoice.paid = '0'   
    ORDER BY finished_invoice.id DESC"

如果有人可以帮我这个。

感谢。

2 个答案:

答案 0 :(得分:0)

而不是比较日期对象使用strtotime()函数来比较为字符串

答案 1 :(得分:0)

您正在将PHP日期格式d-m-y与mysql格式y-m-d进行比较,这就是您遇到问题的原因。 试试这个:

   SELECT *, finished_invoice.date AS invoice_date FROM finished_invoice
        INNER JOIN customer 
        ON finished_invoice.customer = customer.customer_nr 
            WHERE finished_invoice.user = '".$_SESSION['id']."' 
        AND customer.company_name = '$name'
        AND expiry_date < STR_TO_DATE($date, '%d-%m-%Y')
        AND finished_invoice.sent > '0'
        AND finished_invoice.paid = '0'   
        ORDER BY finished_invoice.id DESC