delete 語句帶別名問題.
mysql 8.0 版本:
delete from table1 s
where project_id = ?
and not exists (select project_id from table2 u
where u.project_id = s.project_id
and s.delivery_date = u.use_date)
這個sql 執行一點問題都沒有.
這個sql 在 5.6 或者5.7 上跑是有問題的.
報錯:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;
bad SQL grammar [delete from table1 s
where project_id = ? and not exists (select project_id from table2 u
where u.project_id = s.project_id
and s.delivery_date = u.use_date)];
nested exception is java.sql.SQLSyntaxErrorException: 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 's where project_id = 330 and not exists (select project_id from ijk_project_uplo' at line 1
報錯了. 找不到 s 表.
這個是解析器的一個曲缺陷. 丟掉了 meta資訊 ,不知道 s 是table1 的別名.
修改方法:
1)
delete s from table1 s
where project_id = ?
and not exists (select project_id from table2 u
where u.project_id = s.project_id
and s.delivery_date = u.use_date)
2) 直接去掉 s 子查詢 用table1
delete from table1
where project_id = ?
and not exists (select project_id from table2 u
where u.project_id =table1.project_id
and table1.delivery_date = u.use_date)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-2848072/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【SQL】11 SQL DELETE 語句SQLdelete
- Sqlserver的merge into或delete語句堵塞select語句,鎖型別是LCK_M_ISSQLServerdelete型別
- MySQL 中 DELETE 語句中可以使用別名麼?MySqldelete
- WorkBench,DELETE 標準語句失敗delete
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- DBeaver如何生成select,update,delete,insert語句delete
- MySQL不支援DELETE使用表別名?MySqldelete
- KunlunDB功能之insert/update/delete...returning語句delete
- 比CRUD多一點兒(三):UPDATE、DELETE語句delete
- 輕量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 語句ORMSQLdelete
- mssql sqlserver update delete表別名用法簡介SQLServerdelete
- 翻譯:delete語句(已提交到MariaDB官方手冊)delete
- 50個SQL語句(MySQL版) 問題十四MySql
- [20211221]分析sql語句遇到的問題.txtSQL
- 兩個看似奇怪的MySQL語句問題MySql
- 幽默:演算法只是一堆帶有花哨名稱的 IF ELSE 語句演算法
- [2020528]寫sql語句不要忘記給欄位加上表別名.txtSQL
- MySQL資料庫delete資料時,不支援表別名MySql資料庫delete
- Python中break語句和continue語句有什麼區別?Python
- [20230329]記錄除錯sql語句遇到的問題.txt除錯SQL
- [20211229]toad下優化sql語句注意的問題.txt優化SQL
- java 帶標籤的break,continue語句Java
- flask之控制語句 if 語句與for語句Flask
- MYSQL速度慢的問題 記錄資料庫語句MySql資料庫
- 未使用 `deleteLater` 而直接使用 `delete` 導致問題delete
- [20211229]sql語句包含中文儲存clob的編碼問題.txtSQL
- Windows下Pycharm執行命令列語句結果亂碼問題��Ȩ��Ӧ��WindowsPyCharm命令列
- 20180417PLSQL中sql語句格式化與註解問題SQL
- JavaScript switch與if else語句的區別JavaScript
- 使用別名訪問MSSQL ExpressSQLExpress
- [Laravel系列] 解決laravel中paginate()與distinct() count語句錯誤問題Laravel
- 分支語句和迴圈語句
- 【js迴圈語句】for與while的區別JSWhile
- do-while語句和while的區別While
- 七天帶你玩轉MySQL之SQL語句MySql
- 【解決DML 語句包含不帶 INTO 子句的 OUTPUT 子句】
- flowable 控制檯列印出自帶表的 sql 語句SQL
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase