简体   繁体   中英

what's wrong with this SQL code

I'd like to update all the rows of column URL to Test but I get the following error from the query below

#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 'FROM STx AS a LEFT JOIN Routes AS b ON a.RouteID = b.RouteID WHERE a.GroupID ' at line 3

UPDATE Routes SET URL = 'test' 
WHERE ID in (
    SELECT b.ID
    FROM Stx a left JOIN Routes b on a.RouteID = b.RouteID
    where a.GroupID = 39 and a.Status = 'Provisioned'
);

There's no syntax error here that I can see. I tested it on MySQL 5.5 and the statement parses fine.

I suspect you might have a non-ASCII whitespace character between b.ID and FROM . Try deleting all the spaces and newlines between those two tokens and then re-insert a plain space.

But that doesn't fix the next problem: MySQL doesn't support UPDATE of a table and SELECT from the same table in a single query. So you can't use a subquery in the way you're dong. That's why other answers are suggesting using a multi-table UPDATE.

Another possibility is that you're not sharing the real query you're running. A lot of people on Stack Overflow ask for help with a query, but they have modified the query to post in their question, to make it simpler or to hide proprietary information.

Please don't just say "it doesn't work." That doesn't help us improve our answers. Give the error message, if any, and be sure to show exactly the statement you're typing.

Why not just:

UPDATE Routes a JOIN Stx b ON (a.routeid = b.routeid)
 SET a.URL = 'test'
where b.groupid = 39 and b.status = 'Provisioned'

I created an example SQL Fiddle here.

If you are trying to do something a bit different, can you please either post your real query, or make changes to the data model in the SQL fiddle to show the trouble you are having, and post a link to that.

   UPDATE Routes AS b
    JOIN Stx AS a ON a.RouteID = b.RouteID
    SET b.URL = 'test'
    WHERE a.GroupID = 39 and a.Status = 'Provisioned'

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