最经做项目时发现的问题,好像在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. Version Control,Git的下载与安装

    一.什么是Version Control(版本控制系统)?  ——来自百度百科 以Git为例,是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.Git 是 Linus ...

  2. HBase针对性问题汇总

    Q:    Hbase的rk设计,Hbase优化  a\rowkey:hbase三维存储中的关键(rowkey:行键 ,columnKey(family+quilaty):列键  ,timestamp ...

  3. node中中间件body-parser的实现方式

    最近学习了Koa框架中用到了koa-bodyparser接收表单POST请求的参数,直接使用其API是很容易的,但却不知道其原生方法怎么实现的.故做些笔记 首先,是搭建了Koa的服务器不再赘述 其次, ...

  4. 第零章 HTML启蒙知识与网站开发流程

    Web前端启蒙知识:1.软件架构模式a)B/S架构:Browser-Server 浏览器服务器模型b)C/S架构:Client-Server 客户端服务器模型注1:浏览器是运行网页的应用程序注2:B/ ...

  5. (转)使用yuicompressor-maven-plugin压缩js及css文件(二)

    本文介绍通过使用yuicompressor-maven-plugin插件实现js及css代码的自动压缩,方便集成到持续集成环境中,如jenkins. 一.配置yuicompressor-maven-p ...

  6. WIN32窗口类风格和窗口风格(备查询)

    一.WNDCLASS typedef struct { UINT cbSize //这个结构体的长度,一般用sizeof(WNDCLASSEX)设置 UINT style //窗口式样 WNDPROC ...

  7. 强化学习--QLearning

    1.概述: QLearning基于值函数的方法,不同与policy gradient的方法,Qlearning是预测值函数,通过值函数来选择 值函数最大的action,而policy gradient ...

  8. vm虚拟机出现Disk启动失败

    https://jingyan.baidu.com/article/a501d80c0e45b8ec630f5ed6.html

  9. Java集合-----Set详解

    Set是没有重复元素的集合,是无序的 1.HashSet HashSet它是线程不安全的       HashSet常用方法:   add(E element)  将指定的元素添加到此集合(如果尚未存 ...

  10. Mysql Federated For Windows

    [1]windows环境下打开federated (1)关闭.命令:mysql> net stop mysql (2)添加federated字段.在my.ini文件中添加一个字段,注意位于[my ...