为什么我无法根据不是主键的条件更新列

时间:2010-05-18 10:47:01

标签: sql mysql

为什么我无法根据不是主键的条件更新列。

我正在尝试更新名称与特定标准匹配的选区表,如下所示,但以下查询显示错误


Error code 1064, SQL state 42000: 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 'table constituencies set city_id = '1' where constituencies.name = "East Delhi"' at line 1


update table constituencies set city_id = '1' where constituencies.name = "East Delhi";
update table constituencies set city_id = '1' where constituencies.name = "South Delhi";
update table constituencies set city_id = '1' where constituencies.name = "Delhi Sadar";
update table constituencies set city_id = '1' where constituencies.name = "Karol Bagh";
update table constituencies set city_id = '1' where constituencies.name = "New Delhi";
update table constituencies set city_id = '1' where constituencies.name = "Outer Delhi";
update table constituencies set city_id = '1' where constituencies.name = "North East Delhi";
update table constituencies set city_id = '1' where constituencies.name = "North West Delhi";
update table constituencies set city_id = '1' where constituencies.name = "West Delhi";

是否有必要仅使用主键检查条件?

请注意以上内容。

2 个答案:

答案 0 :(得分:10)

尝试

update constituencies set city_id = 1 where constituencies.name = "East Delhi";

您不需要在mysql查询中编写表。

答案 1 :(得分:3)

您不需要table关键字,就像Salil发布的那样。 另外,请查看function IN了解更新条件 - 它可以简化您的查询(因为您正在更新具有相同ID的行)。

尝试:

UPDATE constituencies set city_id = '1' 
WHERE name IN 
(
  "East Delhi", "South Delhi", "Delhi Sadar", "Karol Bagh", "New Delhi", 
  "Outer Delhi", "North East Delhi", "North West Delhi", "West Delhi"
);