简体   繁体   中英

set column values equals to columnB minus columnA

I want my MySql Column 'Time' to be update as Column 'End_Time' Minus Column 'Start_Time'

Note: End_Time & Start_Time are in DATETIME format....

Thanks

Try this method,

SELECT SEC_TO_TIME(TIMESTAMPDIFF(second,Start_Time,End_Time)) --include sec
or
SELECT SEC_TO_TIME(TIMESTAMPDIFF(minute,Start_Time,End_Time)*60) --diff in minute

eg

SELECT SEC_TO_TIME(TIMESTAMPDIFF(minute,'2016-05-04 10:00:00','2016-05-04 11:29:00')*60)

Here TIMESTAMPDIFF(minute will return the diff in minutes and mutiple it *60 to get seconds, and use SEC_TO_TIME to convert from sec to Time.

Use Datediff function in mysql

See here for DATEDIFF

you can use TIMESTAMPDIFF function.

SELECT TIMESTAMPDIFF(minute, '2015-06-01 13:13:55', '2015-06-03 10:15:20')

SELECT TIMESTAMPDIFF(second, '2015-06-01 13:13:55', '2015-06-03 10:15:20')

SELECT TIMESTAMPDIFF(hour, '2015-06-01 13:13:55', '2015-06-03 10:15:20')
update table 
set Time = (select 
            to_timestamp(End_Time,'yyyy-mm-dd hh24:mi:ss') - to_timestamp(Start_Time,'yyyy-mm-dd hh24:mi:ss')
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM