2012-02-10 47 views
0

num_daysopen返回自打開日期以來的多少天。 剩下的天數返回用戶必須關閉某些東西的天數(它們從打開日期開始到28天)。如果其較長的28天days_left需要默認爲0不是-5或-15等mysql使選擇值默認爲正整數

SELECT 
    DATEDIFF(DATE(NOW()), DATE(dateopened)) AS num_daysopen, 
    (28 - DATEDIFF(DATE(NOW()), DATE(dateopened))) as days_left 
FROM table 

回答

1

使用GREATEST()。

SELECT 
    DATEDIFF(DATE(NOW()), DATE(dateopened)) AS num_daysopen, 
    GREATEST(0,(28 - DATEDIFF(DATE(NOW()), DATE(dateopened)))) as days_left 
FROM table 
+0

這工作,謝謝。 – 2012-02-10 11:53:22