mysql使select值默认为正整数

时间:2012-02-10 09:32:34

标签: mysql select

num_daysopen返回自开放日期以来的天数。 剩余天数返回用户关闭某些内容的天数(从开放日期起最多28天)。如果超过28天days_left需要默认为0而不是-5或-15等

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

1 个答案:

答案 0 :(得分:1)

使用GREATEST()。

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