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,可以课下自行练习实现。

猜你喜欢

转载自blog.csdn.net/qq_16102481/article/details/82969978