简体   繁体   English

这个SQL代码怎么了

[英]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 我想将列URL的所有行更新为Test,但是从下面的查询中收到以下错误

#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. 我在MySQL 5.5上进行了测试,该语句解析良好。

I suspect you might have a non-ASCII whitespace character between b.ID and FROM . 我怀疑您可能在b.IDFROM之间使用非ASCII空格字符。 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. 但这不能解决下一个问题:MySQL不支持在单个查询中更新表和从同一表进行SELECT。 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. 这就是为什么其他答案建议使用多表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. Stack Overflow上的很多人都要求查询的帮助,但是他们已经修改了查询以将其发布到他们的问题中,以使其变得更简单或隐藏专有信息。

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. 我在这里创建了一个SQL Fiddle示例。

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. 如果您尝试做一些不同的事情,可以请您发布实际查询,还是对SQL提琴中的数据模型进行更改以显示您遇到的麻烦,并发布一个链接。

   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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM