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
- Mysql update in報錯 [Err] 1093 - You can't specify target table 'company_info' for update in FROM clauseMySql
- rman: can't open target
- Table is specified twice, both as a target for 'UPDATE' and as a separate source
- Mongoose can't update timestampGo
- RMAN的"rman: can't open target"錯誤
- Mount: you must specify the filesystem type
- [ERROR] Can't open the mysql.plugin tableErrorMySqlPlugin
- I can't truly give chinese sex pills you a conclusive remedyREM
- 2008 5 19: I can't make exception for youException
- linux上“rman can't open target”問題解決Linux
- 關於oracle 10G for suse 9 的rman: can't open targetOracle 10g
- why you can be in netherland
- MySQL報錯Table 'plugin' is read only [ERROR] Can't open the mysql.plugin table.MySqlPluginError
- 完美解決stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.ErrorPython
- MySQL 建立外來鍵報錯Can't write; duplicate key in tableMySql
- linux 掛載光碟:mount: you must specify the filesystem typeLinux
- MYSQL報1022錯誤:Can't write;duplicate key in table '.....'MySql
- Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist解決辦法ErrorMySql
- 【MySQL】ERROR 1093 You canMySqlError
- RMAN備份、恢復實驗室 之 備份篇 【rman: can't open target】
- 解決Error (1133): Can’t find any matching row in the user tableError
- 【MySQL】ERROR 1005 (HY000): Can't create table' (errno: 150)MySqlError
- Laravel Lego :Save you from CRUDLaravelGo
- mysql啟動時報錯Can't read from messagefile errmsg.sysMySql
- You don't know CSSCSS
- ERROR 1005 (HY000): Can't create table 'starive.SC' (errno: 150)"Error
- JavaScript’s “this”: how it works, where it can trip you upJavaScript
- mail can't send title on SolarisAI
- You don't know css(2)CSS
- ERROR 1005 (HY000): Can't create table'matrix.system_log' (errno: 150)Error
- Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not definedWebAPPContextObjectProtocol
- The connection to the server 10.10.0.2:6443 was refused - did you specify the right host or port?Server
- datastage7.5 can't view data?ASTView
- Composer 建立專案 You can also run `PHP --INI`PHP
- Microsoft's AI-powered app can help you learn ChineseROSAIAPP
- S2 - Lesson 57 - Can I help you, madam?
- TypeError: can‘t concat str to bytesError