mysql 的delete from 和update子查询限制
最经做项目时发现的问题,好像在update时也有。。。
网上查到的资料如下:
1.使用mysql进行delete from操作时,若子查询的 FROM 字句和更新/删除对象使用同一张表,会出现错误。
mysql> DELETE FROM tab1 WHERE col1 = ( SELECT MAX( col1 ) FROM tab1 );
ERROR 1093 (HY000): You can’t specify target table ‘tab1′ for update in FROM clause
针对“同一张表”这个限制,撇开效率不谈,多数情况下都可以通过多加一层select 别名表来变通解决,像这样
DELETE FROM tab1
WHERE col1 = (
SELECT MAX( col1 )
FROM (
SELECT * FROM tab1
) AS t
);
或这样
delete from theTable where id in
(
select id from
(
select min(id) id from theTable group by title HAVING count(*)>1
) ids
) ;
------------------------------------------------------------------------
2. mysql delete from where in 时后面 的查询语句里不能加where条件
Sql代码
delete from `t_goods` where fi_id in (select * from ( select fi_id from `t_goods` where fs_num is null and fs_name is null and fs_type is null and fs_using is null and fs_lifetime is null) b)
Sql代码
delete from `t_goods` where fi_id in (select fi_id from `t_goods` where fs_num is null and fs_name is null and fs_type is null and fs_using is null and fs_lifetime is null)
Sql代码
delete from `t_goods` where fi_id in ( select fi_id from `t_goods` )
上面三种情况,只有中间的不能执行。
综合起来就是mysql delete from where in 时后面 的查询语句里不能加where条件
---------------------------------------------------------------------------
3. delete from table... 这其中table不能使用别名
Sql代码
delete from student a where a.id in (1,2);(执行失败)
select a.* from student a where a.id in (1,2);(执行成功)
mysql 的delete from 和update子查询限制的更多相关文章
- mysql update 子查询锁表问题
mysql在Update带有子查询的时候,子查询的表会锁住,导致该表无法使用.比如 update A set comments = (select count(1) from B where id = ...
- MySQL(18):Select- subquery子查询
1. Select- subquery子查询 子查询:是将一条查询语句嵌套在另一条查询语句之中. 2. 案例 需求:查询获得代课天数最多的那个老师的信息. 思路:先获得最多的代课天数是多少天,然后再判 ...
- MySQL学习笔记(五)—— 子查询及联结
子查询: 子查询,即嵌套在其他查询中的查询.例如我们有这样几个表,顾客表,订单表,商品表,我们想知道有哪些客户买了商品A,那么我们就需要先查看哪些订单里包含了商品A,然后根据订单查出是哪些客户. my ...
- MySQL在字段中使用select子查询
前几天看别人的代码中看到在字段中使用select子查询的方法,第一次见这种写法,然后研究了一下,记录下来 大概的形式是这样的: select a .*,(select b.another_field ...
- mysql中的union用法以及子查询综合应用
union查询就是把2条或者多条sql语句的查询结果,合并成一个结果集. 如:sql1: N行,sql2: M行,sql1 union sql2 ---> N+M行 1.能否从2张表查询再uni ...
- MYSQL初级学习笔记六:子查询!(视频序号:初级_42 | 43)
知识点八:子查询(42) 什么是子查询: 子查询是将一个查询语句嵌套在另一个查询语句中.内层查询语句的查询结果,可以作为外层查询语句提供条件. 引发子查询的情况: 使用[NOT] IN 的子查询 -- ...
- MySQL基础(四)(子查询与链接)
1.子查询简介 其中,所谓的“外层查询”并不是指“查找”,指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称SQL. : 2.由比较运算符引发的子查 ...
- MySQL学习之路8-关联子查询
参考文章https://zhuanlan.zhihu.com/p/41844742 成绩表如下Score: 问题:查询科目中成绩大于平均成绩的记录? SELECT * FROM Score WHERE ...
- mysql update 子查询作为条件
UPDATE t_commission_template tctJOIN ( SELECT max(id) maxid FROM t_commission_template WHERE taken_m ...
随机推荐
- c#之函数
1.函数 .函数 /// <summary> /// main /// 主函数(主方法)控制台的输出输入控制函数 /// </summary> /// <param na ...
- mysql基础常用命令
数据库 1查询 Select * From table select host,user,password from mysql.user where user='ybb' and host='%'; ...
- hive-drop-import-delims选项对oracle的clob无效
工作过程中发现了用sqoop将oracle中的数据导入到hive时,会因为oracle中类型为clob的字段中存在换行时,会造成hive的数据错位.即使加上了 --hive-drop-import-d ...
- Python全栈-day8-day9-函数1
函数 day8 1.为什么需要函数 1)代码的组织结构不清晰,可读性差 2)需要重复使用某个功能时,需要重新编写成程序,重复率高 3)多处引用相同代码时,需要扩展功能的时候过于麻烦,工作量大 2.函数 ...
- 随机模拟(MCMC)
http://cos.name/2013/01/lda-math-mcmc-and-gibbs-sampling/ http://blog.csdn.net/lin360580306/article/ ...
- css 箭头
.toTop{ width: 2.5rem; height: 2.5rem; background-color: rgba(228,228,228,.6); position: fixed; bott ...
- win10 Xshell5连ubuntu服务器
相当于ubuntu做服务器,win10远程访问,同一局域网下,完全参考度娘 教你怎么使用xshell远程连接linux服务器 ubuntu系统ifconfig -a查看ip地址,添加到主机那里,连接时 ...
- 开源数据流管道-Luigi vs Azkaban vs Oozie vs Airflow
原文链接:https://www.jianshu.com/p/4ae1faea733b 随着企业的发展,他们的工作流程变得更加复杂,越来越多的有着错综复杂依赖关系的工作流需要增加监控,故障排除.如果没 ...
- mybatis源码解析9---执行器Executor解析
从前面分析我们知道了sql的具体执行是通过调用SqlSession接口的对应的方法去执行的,而SqlSession最终都是通过调用了自己的Executor对象的query和update去执行的.本文就 ...
- leaflet:调用arcgis切片地图服务
var mymap = L.map('mapid').setView([31.59, 120.29], 7); L.tileLayer('http://map.geoq.cn/ArcGIS/rest/ ...