简体   繁体   中英

MySQL query syntax with update and inner join

I have been trying to update 1 table based on a value in another table (and there are corresponding Identifers). I simply cannot work out what the syntax is for this, and believe I may have a mix of MS-SQL and MySQL, though very new to this type of coding so having issues unravelling this one.

I am trying to get this query working in MySQL

UPDATE timetable As T
SET T.TrainsArrived = T.TrainsArrived + A.TrainsToday
INNER JOIN 
(
    SELECT TrainID, TrainsToday
    FROM TrainTracker
) A ON A.TrainID = T.TrainID;

The issue that the MySQL console is telling me about is on the word 'INNER' and that it is expecting EOF or ';'

Any assistance would be greatly appreciated!

Use the following syntax, and note that the subquery doesn't seem to even be necessary here:

UPDATE timetable AS T
INNER JOIN TrainTracker AS TT
    ON TT.TrainID = T.TrainID
SET TrainsArrived = T.TrainsArrived + TT.TrainsToday;

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