1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQL Error 1093 You can't specify target table....

Posted at

MySQL error 1093

あるレコードを同じテーブルの別レコードの内容でアップートしたいことがあったが表題のエラーに遭遇した

MySQL バージョン

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper
$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

NG だった SQL 文

mysql > UPDATE details SET dettail_name=(SELECT detail_name FROM details WHERE id=1) WHERE id=2;
ERROR 1093 (HY000): You can't specify target table 'details' for update in FROM clause

mysql error 1093 でググる
どうやらサブクエリの FROM 句と更新対象のテーブルが同じだとダメらしい
AS 使うと回避できるということで

OK だった SQL 文

mysql > UPDATE details SET dettail_name=(SELECT D.detail_name FROM (SELECT detail_name FROM details WHERE id=1) AS D) WHERE id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

うまくいった…

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?