Mysql - 保修到期了吗?

时间:2013-09-01 11:39:03

标签: mysql date mysqli

一个小的MySQL查询; - )

我有两个值,一个InvoiceDate(格式:YYYY-MM-DD)和一个WarrantyPeriod(格式:nn)(保修年数)

一个简单的查询来选择保修期满的项目?

我试过这个:

SELECT
*
FROM
(
    SELECT
        UNIX_TIMESTAMP(DATE_FORMAT(item_invoice_date, '%Y-%m-%d %H:%i:%s')) AS ux_id,
        (item_warranty_period * 365 * 86400) AS ux_wp
    FROM
        items
    WHERE
        items.item_warranty_period > 0
    AND
        items.item_invoice_date != '0000-00-00'
) wd
,items
WHERE
items.item_warranty_period > 0
AND
items.item_invoice_date != '0000-00-00'
AND
wd.ux_id - wd.ux_wp < UNIX_TIMESTAMP(NOW())
GROUP BY
items.item_id

但似乎没有工作; - )

2 个答案:

答案 0 :(得分:1)

SELECT
  *
FROM
  items
WHERE
  NOW() > (invoice_date + INTERVAL warranty_period YEAR)

答案 1 :(得分:0)

我的查询并不能满足您的需求,但您在寻找这个吗?

SELECT * FROM table WHERE ADDDATE(InvoiceDate, INTERVAL WarrantyPeriod YEAR) < NOW()

请参阅:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html