最经做项目时发现的问题,好像在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. 将网站项目转为 Web form应用程序(转)

    转自 http://blog.sina.com.cn/s/blog_53729e4601014ze9.html 本文介绍如何将现有的 Microsoft Visual Studio 2005 网站项目 ...

  2. ReentrantLock源码(一)

    一.简介. ReentrantLock 是一个互斥锁,在基本行为和机制上与synchonized一样,只不过synchonized用方法和声明访问了隐式的锁监视器,但是ReentrantLock 做了 ...

  3. Redis实现分布式Session

    相关博客: http://www.cnblogs.com/yanweidie/p/4763556.html http://www.cnblogs.com/lori/p/5368722.html?utm ...

  4. arc 093 C – Traveling Plan

    题意: 给出横坐标上一系列的点,一个人从0出发按照下标顺序访问每一个点,再回到0点. 问每次如果去掉一个点,那么访问的距离变为多少. 思路: 去掉这个点,那么就减去这个点到上一点到这一点的距离,减去这 ...

  5. codeforces 768c Jon Snow And His Favourite Number

    题意: 给出一个数列,和一种操作,以及两个数x和k. 这个操作有两个步骤: 首先把这个数列按照升序排序,然后把所有奇数位上的数字与x异或. 问执行k次操作之后,这个数列的最大值和最小值是多少. 思路: ...

  6. QTCreator 调试:unknown debugger type "No engine"

    [1]QTCreator调试,应用程序输出:unknown debugger type "No engine" 如图:下断点->调试程序->应用程序输出 说明:调试器无 ...

  7. Second LearningConvolutionalNeuralNetworksforGraphs Experience

    paper +ppt 链接:https://pan.baidu.com/s/1ZLBvv7mP8OoseQ4tnwhr_A 提取码:4amg

  8. Hive复制分区表和数据

    1. 非分区表: 复制表结构: create table new_table as select * from exists_table where 1=0; 复制表结构和数据: create tab ...

  9. go语言,golang学习笔记4 用beego跑一个web应用

    go语言,golang学习笔记4 用beego跑一个web应用 首页 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/ 更新的命令是加个 -u 参数,g ...

  10. TensorFire:WEB端的高性能神经网络框架

    TensorFire:WEB端的高性能神经网络框架 摘要: 近日,一种专门用于在网页内执行神经网络算法的JavaScript库——TensorFire引起了人们的关注,这种JavaScript库在浏览 ...