简体   繁体   中英

I want Age 0 when the date is 0 in mysql query

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

Above is my mysql query where if i consider MANUFACTURING_DATE =0 then it results Age as 117, i want some solution where if the MANUFACTURING_DATE =0 it should result the Age as 0.

You can use a CASE stattement:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM