You can‘t specify target table ‘Person‘ for update in FROM clause
題目:編寫一個 SQL 查詢,來刪除 Person 表中所有重複的電子郵箱,重複的郵箱裡只保留 Id 最小 的那個。
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id 是這個表的主鍵。
例如,在執行你的查詢語句之後,上面的 Person 表應返回以下幾行:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
提示:
執行 SQL 之後,輸出是整個 Person 表。
使用 delete 語句。
報錯答案:
delete from Person
where Id not in
(select min(id) from Person
group by Email)
執行這條語句時會報錯:You can’t specify target table ‘Person’ for update in FROM clause
這是因為MySQL不允許同時查詢和刪除一張表,我們可以通過子查詢的方式包裝一下即可避免這個報錯
delete from Person
where Id not in
(select id from
(select min(id) as id from Person
group by Email) as t
)
相關文章
- mysql中You can’t specify target table for update in FROM clMySql
- Table is specified twice, both as a target for 'UPDATE' and as a separate source
- Mongoose can't update timestampGo
- FILESTREAM feature can't be enabled if you use cluster shared volumes
- MYSQL報1022錯誤:Can't write;duplicate key in table '.....'MySql
- 完美解決stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.ErrorPython
- why you can be in netherland
- MySQL報錯Table 'plugin' is read only [ERROR] Can't open the mysql.plugin table.MySqlPluginError
- The connection to the server 10.10.0.2:6443 was refused - did you specify the right host or port?Server
- 阿里雲 Redis 報出You can't read against a non-read redis.解決方案阿里RedisAI
- You don't know CSSCSS
- [leetcode] 1642. Furthest Building You Can ReachLeetCodeUI
- In the meantime you can read the IGN analysis of Madden 22
- LeetCode之All Paths From Source to Target(Kotlin)LeetCodeKotlin
- You don't know css(2)CSS
- SAP MM The Table MBEWH Update Logic
- TypeError: can‘t concat str to bytesError
- Can't open file: (errno: 24)
- Extract rows from a CORRUPT table creating ROWID from DBA_EXTENTS
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- Composer 建立專案 You can also run `PHP --INI`PHP
- S2 - Lesson 57 - Can I help you, madam?
- Incorrect MEMORY_MAX_TARGET (> Available RAM) Can Lead To Database HangsAIDatabase
- Supervisor 解決 can't find command *
- SSM+Swagger問題解決 Can‘t read swagger JSON from http://localhost:8080/xxx/swagger/api-docsSSMSwaggerJSONHTTPlocalhostAPI
- eslint --fix 報錯 can't not find modulesEsLint
- mysql遇到Variable can’t be set to the value of ‘NULL’MySqlNull
- MySQL 2003 - Can’t connect to MySQL server on (10060)MySqlServer
- Can't load IA 32-bit .dll on a
- multiprocessing pool AttributeError: Can't get attribute 'func'Error
- 網站提示:You Don’t Have Permission To Access網站
- 關於Warning: setState(...): Can only update a mounted or mounting component. This
- Python——UnicodeEncodeError: 'ascii' codec can't encode/decode charactersPythonUnicodeErrorASCII
- redis : Can't save in background: fork: Cannot allocate memoryRedis
- HDU 4027 Can you answer these queries? (線段樹 區間開方)
- use database 切換提示You can turn off this feature to get a quicker startupDatabaseUI
- PostgreSQL DBA(71) - Locks(Table-Level):What You Should KnowSQL
- You don't have write permissions for the /usr/bin directory.