简体   繁体   English

sql 更新where子查询

[英]sql update where subquery

My exercise is:我的练习是:

The actor HARPO WILLIAMS was accidentally entered in the actor table as GROUCHO WILLIAMS.演员 HARPO WILLIAMS 意外地以 GROUCHO WILLIAMS 的身份进入了演员表。 Write a query to fix the record.编写查询以修复记录。

so I am trying to query it by:所以我试图通过以下方式查询它:

update actor
 set actor.first_name = 'HARPO'
 where actor_id in (
  select actor.actor_id from actor
  where concat(actor.first_name, ' ', actor.last_name) = 'GROUCHO WILLIAMS'
 );

unfortunately error is thrown不幸的是,错误被抛出

You can't specify target table 'actor' for update in FORM clause您不能在 FORM 子句中指定目标表“演员”进行更新

How can I fix that?我该如何解决?

You don't need subquery for this;您不需要子查询; just match the first_name and last_name separately to get the row to update.只需分别匹配 first_name 和 last_name 以获取要更新的行。 Following query can also utilize the benefit of composite index (first_name, last_name) (if defined) on the actor table:以下查询还可以利用actor表上的复合索引(first_name, last_name) (如果已定义)的好处:

update actor
set actor.first_name = 'HARPO'
where actor.first_name = 'GROUCHO' 
       AND actor.last_name = 'WILLIAMS'

Dont use subquery for this, you can use simple update or else like this,不要为此使用子查询,您可以使用简单的更新或者像这样,

UPDATE actor AS s, (SELECT id  FROM actor WHERE fname='GROUCHO' AND lname = 'WILLIAMS') AS p
SET s.fname = 'HARPO' 
WHERE s.id = p.id;

DB FIDDLE LINK: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5ceb8e0d5d5837ce2ec13f18bfd103b2 DB FIDDLE 链接: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5ceb8e0d5d5837ce2ec13f18bfd103b2

Just match the first name and the last names seperately to update the row.只需分别匹配名字和姓氏即可更新行。 I don't think the subquery is important.我不认为子查询很重要。

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

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