最经做项目时发现的问题,好像在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子查询限制的更多相关文章

  1. mysql update 子查询锁表问题

    mysql在Update带有子查询的时候,子查询的表会锁住,导致该表无法使用.比如 update A set comments = (select count(1) from B where id = ...

  2. MySQL(18):Select- subquery子查询

    1. Select- subquery子查询 子查询:是将一条查询语句嵌套在另一条查询语句之中. 2. 案例 需求:查询获得代课天数最多的那个老师的信息. 思路:先获得最多的代课天数是多少天,然后再判 ...

  3. MySQL学习笔记(五)—— 子查询及联结

    子查询: 子查询,即嵌套在其他查询中的查询.例如我们有这样几个表,顾客表,订单表,商品表,我们想知道有哪些客户买了商品A,那么我们就需要先查看哪些订单里包含了商品A,然后根据订单查出是哪些客户. my ...

  4. MySQL在字段中使用select子查询

    前几天看别人的代码中看到在字段中使用select子查询的方法,第一次见这种写法,然后研究了一下,记录下来 大概的形式是这样的: select a .*,(select b.another_field ...

  5. mysql中的union用法以及子查询综合应用

    union查询就是把2条或者多条sql语句的查询结果,合并成一个结果集. 如:sql1: N行,sql2: M行,sql1 union sql2 ---> N+M行 1.能否从2张表查询再uni ...

  6. MYSQL初级学习笔记六:子查询!(视频序号:初级_42 | 43)

    知识点八:子查询(42) 什么是子查询: 子查询是将一个查询语句嵌套在另一个查询语句中.内层查询语句的查询结果,可以作为外层查询语句提供条件. 引发子查询的情况: 使用[NOT] IN 的子查询 -- ...

  7. MySQL基础(四)(子查询与链接)

    1.子查询简介 其中,所谓的“外层查询”并不是指“查找”,指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称SQL. : 2.由比较运算符引发的子查 ...

  8. MySQL学习之路8-关联子查询

    参考文章https://zhuanlan.zhihu.com/p/41844742 成绩表如下Score: 问题:查询科目中成绩大于平均成绩的记录? SELECT * FROM Score WHERE ...

  9. mysql update 子查询作为条件

    UPDATE t_commission_template tctJOIN ( SELECT max(id) maxid FROM t_commission_template WHERE taken_m ...

随机推荐

  1. 21Oracle数据库和实例

    Oracle数据库:相关的操作系统文件(即储存在计算机硬盘上的文件)的集合,这些文件组织在一起,成为一个逻辑整体,即为Oracle数据库.物理存在 Oracle实例:位于物理内存里的数据结构,它由操作 ...

  2. django admin 处理文本换行空格

    使用 linebreaks filter <p>{{ blogpassage.content|linebreaksbr }}<p>

  3. jQuery安装

    http://www.runoob.com/jquery/jquery-install.html 网页中添加jQuery: 方法一:可以从http://jquery.com/download/ 下载j ...

  4. JavaBean和List<JavaBean>

    2018-11-04 23:04:03开始写 返回泛型为User是列表 public List<User> getUserInfo() { conn = getConn();//获取数据库 ...

  5. CSS实例:图片导航块

    1.认识CSS的 盒子模型. 2.CSS选择器的灵活使用. 3.实例: a.图片文字用div等元素布局形成HTML文件. b.新建相应CSS文件,并link到html文件中. c.CSS文件中定义样式 ...

  6. qDeleteAll 之后必须清空容器

    [1]qDeleteAll应用示例 qDeleteAll源码如下: template <typename ForwardIterator> Q_OUTOFLINE_TEMPLATE voi ...

  7. APIView源码解析

    1.首先安装pip install djangorestframework 2.导入from rest_framework.views import APIView class Courses(API ...

  8. python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出

    想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数.它会使得输出和pprint() 函数效果类似 >>> data {'age': 4, 'nam ...

  9. 模拟windows全盘搜索

    循环遍历pc上的文件夹,保存到mysql数据库中,搜索时,从数据库取数据.import osimport datetimeimport pymysqlimport threading def link ...

  10. python二叉树的深度遍历之先序遍历流程图