简体   繁体   中英

Time difference between two columns - mysql

I need to do 2 separate queries and need the result. one is where i need the difference between values of 2 columns in 2 different tables.

table 1
user id | userroomid | roomid | createdOn
2           22          54       2018-07-13 03:58:33
3           23          55       2018-07-13 04:58:33
4           26          59       2018-08-13 04:58:33


table 2
user id | roomname | roomid | createdTime
2           asd          54       2018-07-13 03:58:33
3          deg         55       2018-07-13 04:58:33
4           bfds          59       2018-08-13 04:58:33

so, I want the time difference between the column createdTime and createdOn from the 2 tables.

and I have already written the other query which is to insert items into a table, but I am not sure how to join these 2 queries and make them one.

here's the 2nd query

 let sqlInsert = mysql.format(`INSERT INTO userroom (roomid,userid) VALUES (?, ? )`,[roomid,userid]);

So, how do I accomplish this?

join between that two tables then use TIMEDIFF function which expressed as a time value and TIME_TO_SEC it will convert time in second

Example of TIMEDIFF` function

   SELECT TIME_TO_SEC( TIMEDIFF('2009-05-18 15:45:57.005678','2009-05-18 13:40:50.005670'));

it will return 7507 sec

For your case

select TIME_TO_SEC( TIMEDIFF(t1.createdOn,t2.createdOn)) as time_diff from table1 t1 
inner join  table2 t2 on  t1.user id=t2.user id

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