网站首页 > 技术文章 正文
在使用MySQL过程中,增删改查是不可避免的操作,在删除delete操作时,有时会使用到子查询,如先获取要删除的id,再进行删除操作,下面的SQL就实现了该功能:
delete from tableA where id in ( select id from test)
但在执行时,发现该语句执行效率非常低,虽然test表中并没有多少条数据(100多条),但执行了几分钟也是可能的。如果改为关联方式的删除,效率将提升明显:
delete a from tableA a inner join test b on a.id =b.id
另外,如果在查询中使用的表名和删除表相同,还会返回错误信息:
delete from tableA where id in ( select max(id) from tableA GROUP BY custid HAVING count(0)>1 )
会返回错误的信息:
Table 'tableA' is specified twice, both as a target for 'DELETE' and as a separate source for data
这时就需要在子查询中再加一层,使子查询中使用类似临时表的方式。
delete from tableA where id in ( select id from (select max(id) as id from tableA GROUP BY custid HAVING count(0)>1 ) t )
猜你喜欢
- 2024-09-18 SQLSERVER:INSERT、UPDATE、DELETE操作
- 2024-09-18 牛逼!2021 最新 Spring 面试题(spring面试题汇集与答案)
- 2024-09-18 为什么 MySQL 不推荐使用 join?(为啥mysql)
- 2024-09-18 VBA数组与字典解决方案第27讲:两列数据相互去掉重复值后合并
- 2024-09-18 在Neo4j中怎样使用APOC函数来过滤字典中的Key
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)