我希望在mysql查询中日期为0时为0

时间:2017-10-27 05:33:06

标签: php mysql

SELECT (GREATEST((YEAR(NOW()) - GREATEST( M.MANUFACTURING_DATE,1900)), 0))
AS AGE,M.MANUFACTURING_DATE FROM table_name M;

上面是我的mysql查询,如果我认为MANUFACTURING_DATE = 0然后它将Age定为117,我想要一些解决方案,如果MANUFACTURING_DATE = 0,它应该使Age为0。

1 个答案:

答案 0 :(得分:0)

您可以使用CASE状态:

SELECT 
    CASE 
        WHEN M.MANUFACTURING_DATE=0 THEN 0
        ELSE (GREATEST((YEAR(NOW()) - GREATEST( M.MANUFACTURING_DATE,1900)), 0))
    END
    AS AGE,M.MANUFACTURING_DATE 
FROM table_name M;