MySQL calculates the difference between the end time and the start time, and counts the difference

1. SQL

--方法一:将查询的结果作为一个新的表
--注意:要给新的表取一个别名,不然会报 Every derived table must have its own alias 错误
select SUM(time) as result FROM((select TIMESTAMPDIFF(SECOND,t.start_time,t.end_time) as time FROM test_time t ) as a )
--方法二:
select SUM(TIMESTAMPDIFF(SECOND,t.start_time,t.end_time) ) as time FROM test_time t 

2. TIMESTAMP DIFF function

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Description: This expression returns the difference datetime_expr1with time, where the sum can be a DATE or DATETIME expression. The argument is the unit for determining the result, expressed as an integer. Valid units are: MICROSECOND microsecond SECOND second MINUTE minute HOUR hour DAY day WEEK week MONTH month QUARTER YEAR yeardatetime_expr2datetime_expr1datetime_expr2
unit









PS: Sogou input method switches between traditional and simplified characters.
When I was writing, I suddenly found that the input method has changed to traditional characters

Solutions :
1. Computer version: Right-click Sogou Input Method, enter Settings, click the Common tab in the property settings, select "Simplified" in the default state, and click OK to switch to Simplified input.
You can also use shortcut keys on the computer to change, the default shortcut key is to press Ctrl, Shift and F keys at the same time.
2. Mobile version: Open Sogou Input Method, click the Settings button, enter the Input Settings and find the Simplified and Traditional Chinese setting option, and check the Simplified Chinese setting.

Reference Article 1
Reference Article 2
Reference Article 3

Guess you like

Origin blog.csdn.net/wyzyysw/article/details/113655191