1. 程式人生 > >MySQL if 函式實現類似Oracle decode函式功能

MySQL if 函式實現類似Oracle decode函式功能

SQL

SELECT if(timestampdiff(hour, sa.add_time, now()) < 1000, if(timestampdiff(hour, sa.add_time, now()) < 300, if(timestampdiff(hour, sa.add_time, now()) < 128, 3 , 2 ) , 1 ) , 0 ) diftime from equipdata sa order by diftime desc

3,2,1,0 為返回結果 此sql為查詢兩時間差, 以hour為單位。 hour可以替換成minute,day,month,可以課下自行練習實現。