What's wrong with my MySQL statement?

2019-08-20 04:32发布

问题:

UPDATE table1 SET announcer = ( SELECT memberid
FROM ( table1
JOIN users ON table2.username = table1.announcer
) AS a
WHERE a.username = table1.announcer )

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where a.username=table1.announcer)' at line 1

回答1:

Try:

UPDATE announcements a
SET announcer =
(SELECT memberid
FROM users u
WHERE u.username = a.announcer)


回答2:

You can also do the JOIN in the UPDATE

UPDATE announcements JOIN users
SET announcements.announcer=users.memberid
WHERE announcements.username=users.username;

Note: For safty reasons (until your sure announcers get copied over right) I'd instead create a new column, say announcerNew then

UPDATE announcements JOIN users
SET announcements.announcerNew=users.memberid
WHERE announcements.username=users.username;