简体   繁体   中英

MySQL Update with Select statement from the same table

I have a table called time_table with the following columns

--------------------------
 time_table
--------------------------
col_name | type
id       | int
user_time| datetime

This table has record in it. When I updated the table to :

--------------------------
 time_table
--------------------------
col_name | type
id       | int
user_time| datetime
utc_time | datetime

I want to Select all the user_time column, get its UTC equivalent and Update the utc_time column in one query.

I tried this query but it didn't work.

UPDATE time_table p1, ( 
SELECT id, CONVERT_TZ(user_time,'+00:00','+05:00') AS newtime
FROM time_table sp WHERE sp.id=p1.id) AS p2
SET p1.utc_time = p2.newtime
WHERE p1.id = p2.id

I know there is something wrong with my query but I don't know what's wrong with it. Any suggestions? It would be a great help.

You don't need to join

UPDATE time_table 
SET utc_time = CONVERT_TZ(user_time, '+00:00','+05:00')

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