我遇到了一些MySQL選擇查詢的意外行爲。我正在查詢:意外的結果 - 基於日期比較的MySQL SELECT
SELECT `refno`, `subdomain`, `toplevels`, `renew_until`, `expiry_date`,
(YEAR(`renew_until`) - YEAR(`expiry_date`)) AS `renew_for` FROM `testing_names`
WHERE `expiry_date` >= DATE(NOW()) AND `renew_for` >= 0
返回(如預期):
| refno | subdomain | toplevels | renew_until | expiry_date | renew_for |
|-----------------------------------------------------------------------------------|
| 5 | domain1 | com | 2014-02-02 | 2014-02-02 | 0 |
| 45 | domain2 | net | 2014-01-27 | 2013-01-27 | 1 |
但是下面的查詢(注意renew_for
不同的比較)返回一個空集:
SELECT `refno`, `subdomain`, `toplevels`, `renew_until`, `expiry_date`,
(YEAR(`renew_until`) - YEAR(`expiry_date`)) AS `renew_for` FROM `testing_names`
WHERE `expiry_date` >= DATE(NOW()) AND `renew_for` > 0
在這種情況下,我期待行#45;我的查詢出了什麼問題?我是否正確使用renew_for
?