1

我正在尝试制定一个看似简单的 SQL 查询,在该查询中我连接两个表,并根据其中一列的日期差异对结果进行排序,并且NOW().

我正在尝试查询:

SELECT advertise_id, 
       qr_startdate,
       qr_enddate, 
       DATEDIFF(day, NOW(), t1.qr_enddate) AS d 
FROM `adv_qr` t1 
INNER JOIN advertise_table t2 
    ON t1.advertise_id = t2.lid 
ORDER BY t1.d ASC

这似乎应该是正确的,但显然语法中的某些内容不正确。我一直在尝试各种事物的组合,但似乎无法以DATEDIFF一种我可以用它排序结果的方式返回。

4

3 回答 3

5
SELECT advertise_id, 
       qr_startdate,
       qr_enddate, 
       DATEDIFF(NOW(), t1.qr_enddate) AS d 
FROM `adv_qr` t1 
INNER JOIN advertise_table t2 
    ON t1.advertise_id = t2.lid 
ORDER BY d ASC
于 2013-08-28T17:54:39.277 回答
1

Check out this SQLFiddle:

SELECT advertise_id, 
       qr_startdate,
       qr_enddate, 
       DATEDIFF(NOW(), t1.qr_enddate) AS d 
 FROM `adv_qr` t1 
 INNER JOIN advertise_table t2 
    ON t1.advertise_id = t2.lid 
 ORDER BY d ASC

You can find the MySQL DATEDIFF documentation here.

于 2013-08-28T17:59:40.643 回答
0

使用直接减法怎么样?

NOW() - t1.qr_enddate

作为排序标准?

于 2013-08-28T17:54:43.587 回答