sql - Create SQL column that subtracts two date columns
问题描述
This is how my columns look.
I want to create a column that contains the differences between the start time and end time columns.
Start_Time 2019-02-01 22:38:54.0000000 2019-02-01 22:39:18.0000000 2019-02-01 22:44:43.0000000 2019-02-04 22:17:41.0000000 2019-02-04 22:18:09.0000000
End_Time 2019-02-01 22:38:57.0000000 2019-02-01 22:39:28.0000000 2019-02-01 22:44:44.0000000 2019-02-04 22:17:48.0000000 2019-02-04 22:18:21.0000000
Any help would be greatly appreciated!
解决方案
句法:
DATEDIFF(expr1,expr2)
TIMEDIFF(expr1,expr2)
描述:
DATEDIFF() 返回 (expr1 – expr2),表示为从一个日期到另一个日期的天数。expr1 和 expr2 是日期或日期和时间表达式。计算中仅使用值的日期部分。
TIMEDIFF() 返回 (expr1 - expr2) 表示为时间值。expr1 和 expr2 是时间或日期和时间表达式,但两者必须属于同一类型。
SELECT DATEDIFF(Start_Time, End_Time) AS DateDiff from table_name
资料来源:MariaDB DATEDIFF只接受两个参数。它根据 MySQL 实现而变化。
推荐阅读
- javascript - Breakout game JavaScript - 有时球会直接穿过而不是碰撞
- amazon-web-services - 从 AWS Cloudian Object Store 获取文件大小
- javascript - d3水平图表显示虚假数据
- c++ - 递归程序的执行流程
- symfony - 如何使用教义和 symfony 添加有关多对多关系的更多信息?
- c++ - 如何在工厂方法设计模式中返回子类的实例?
- oracle-apex - 没有 url 访问 oracle apex 页面
- sql - 当“组”没有记录时显示每日计数为零
- c# - 与 OnCreated 相对的 Linq to Sql
- mongodb - MongoDB 聚合中的 $$ROOT 是什么以及它是如何工作的?