【SQL语句】求时间差的函数-timestampdiff() 以及取绝对值(abs)
一、timestampdiff(unit,begin,end);
timestampdiff函数返回begin - end的结果,其中begin和end是date或datetime表达式。
timestampdiff函数允许其参数具有混合类型,例如,begin是date值,end可以是datetime值。
如果使用date值,则timestampdiff函数将其视为时间部分为“00:00:00”的datetime值。
例:
> select timestampdiff(minute,'2019-01-01 10:00:00','2019-01-02 10:00:00') result;
+--------+
| result |
+--------+
| 1440 |
+--------+
> select timestampdiff(day,'2019-01-01 10:00:00','2019-01-02 19:00:00') result;
+--------+
| result |
+--------+
| 1 |
+--------+
注:
- microsecond -微秒
- second —— 秒
- minute —— 分钟
- hour —— 小时
- day ———— 天
- week —— 周
- month —— 月
- quarter —— 季度
- year —— 年
二、取绝对值——abs()
> select abs(-1.1) result;
+--------+
| result |
+--------+
| 1.1 |
+--------+
注:
返回值的类型和参数的类型一致