简体   繁体   中英

mysql update entire column with column from select query

I have Date and Time column separated in the existing database and now I want to merge those two columns in one DateTime column with UTC TimeZone. I get retrieve column of converted time with datetime with following query

(SELECT convert_tz(t1, @@session.time_zone, '+00:00') FROM (SELECT CONCAT(`cDate`, ' ', `cTime`) AS t1 FROM testTable2) AS t2)

And now I want to update one of the columns in the same table.

UPDATE `testtable2` SET cDateTime = (SELECT convert_tz(t1, @@session.time_zone, '+00:00') FROM (SELECT CONCAT(`cDate`, ' ', `cTime`) AS t1 FROM testTable2) AS t2)

This won't work because it's expecting one row. How do I do this without using any procedure calls or functions? Thanks for your help!

I can't test right now (I have issues with my local MySQL installation), but I see no reason why you couldn't simply do:

UPDATE testtable2 SET cDateTime = convert_tz(CONCAT(cDate, ' ', cTime), @@session.time_zone, '+00:00')

Also, if that query is expected to be run on large number of rows, or very frequently, then it would payoff not to go through string generation/concatenation/parsing, but instead use date time arithmetics...

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