mysql_交换指定两条记录的某个字段的值
来源:互联网 发布:移动宽带是什么网络 编辑:程序博客网 时间:2024/05/09 20:29
今天在用mysql操作时遇到了下面的问题 : You can't specify target table ‘quenn’ for update in FROM clause
原因:mysql不能先select出同一表中的某些值,再update这个表(在同一语句中)。
我的解决方法:
方法一:
mysql语句如下:
update question set sort=(case when id=7 then (select a.sort from (select tmp.* from question tmp) a
where a.id=8) when id=8 then (select a.sort from (select tmp.* from question tmp) a where a.id=7) end)
where id=7 or id=8;
方法二:
update question as q1 join question as q2 on (q1.id=7 and q2.id = 8)
or(q1.id = 8 and q2.id=7)
set q1.sort = q2.sort,q2.sort=q1.sort;
以下为所查资料:
资料一:http://www.cntxk.com/CataNews/52/info7286.html
今天在写 mysql 遇到一个比较特殊的问题。
mysql 语句如下:
update wms_cabinet_form set cabf_enabled=0
where cabf_id in (
SELECT wms_cabinet_form.cabf_id FROM wms_cabinet_form
Inner Join wms_cabinet ON wms_cabinet_form.cabf_cab_id = wms_cabinet.cab_id
Inner Join wms_cabinet_row ON wms_cabinet.cab_row_id =wms_cabinet_row.row_id
where wms_cabinet_row.row_site_id=27 and wms_cabinet_form.cabf_enabled=1)
运行时提出如下提示: You can't specify target table 'wms_cabinet_form' for update in FROM clause
运行 in 里面的 select 字句:
SELECT wms_cabinet_form.cabf_id FROM wms_cabinet_form
Inner Join wms_cabinet ON wms_cabinet_form.cabf_cab_id = wms_cabinet.cab_id
Inner Join wms_cabinet_row ON wms_cabinet.cab_row_id =wms_cabinet_row.row_id
where wms_cabinet_row.row_site_id=27 and wms_cabinet_form.cabf_enabled=1
可以正确 select 正确结果。再把结果直接写到 in 里面,改后语句如下:
update wms_cabinet_form set cabf_enabled=0 where cabf_id in ('113','114','115'),再运行可以正确执行更新。
到这一步开始想不明白,为什么用 select 子句运行会出错呢?以前在 mssql 这种写法是很常见的。
没办法了,唯有动用 baidu。找到两条记录。
原来原因是:mysql中不能这么用。 (等待mysql升级吧)。那串英文错误提示就是说,不能先select出同一表中的某些值,
再update这个表(在同一语句中)。 也找到替代方案,重写改写了 sql 。
改写后的 sql 如下所示,大家仔细区别一下。
update wms_cabinet_form set cabf_enabled=0 where cabf_id in (
SELECT a.cabf_id FROM (select tmp.* from wms_cabinet_form tmp) a
Inner Join wms_cabinet b ON a.cabf_cab_id = b.cab_id
Inner Join wms_cabinet_row c ON b.cab_row_id = c.row_id
where c.row_site_id=29 and a.cabf_enabled=1)
重点在 SELECT a.cabf_id FROM (select tmp.* from wms_cabinet_form tmp) a ,我 select tmp.* from wms_cabinet_form tmp 作为子集,
然后再 select a.cabf_id FROM 子集,这样就不会 select 和 update 都是同一个表。致此问题得到完美解决。
参考文章:
http://zhidao.baidu.com/question/68619324.html
http://blog.163.com/xiaoqiu_1120/blog/static/121632322007112411424982/
资料二:http://zhoupuyue.iteye.com/blog/842126
交换两条记录中的字段值方法
- 博客分类:
- Oracle
- 表test:
- id priority
- 1 1
- 2 2
- update test set priority=
- (case when id=1 then
- (select priority from test where id=2)
- when id=2 then
- (select priority from test where id=1) end)
- where id=1 or id=2;
- mysql_交换指定两条记录的某个字段的值
- 数据库中交换两条记录字段值的方法
- 一个sql语句同时实现两条数据的某个字段值交换
- 拼接多条记录的某个字段
- 忆龙2009:自动计算相邻两条记录指定字段的差值
- mysql 数据库,更换两条记录对应字段的值。
- 怎么根据一条记录中某个字段的值,将此记录分成多条显示?
- MYSQL 查询前N条记录某个字段的和
- 给某个条重复记录的 某个字段 提供各自的 可填区间
- 一张表里,不同字段,值相同的两条记录,查询这两条记录中不同字段的语句
- sql取某个字段相近某个值的记录
- Oracle两张数据表之间某个指定字段的单向和双向同步
- SQL 替换指定字段某个位置的值
- solr原子更新-即指定更新某个字段的值
- mysql_根据不同的字段值替换不同的值
- 数据库查询字段某个值附近的10条数据
- 选择某个属性值最大的那条记录(不仅仅包含指定属性,而是想要什么属性都可以)
- 【笔记】mysql两条数据的某个属性值互换
- JavaScript delete操作符详解
- Delphi TIniFile 用法
- 关于拷贝构造函数和重载成员运算符=
- java集合中对象某属性比较排序Comparable与Comparator
- 让交叉编译就像本机编译一样简单:Scratchbox2的使用
- mysql_交换指定两条记录的某个字段的值
- SD卡文件系统(转自百度文库)
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- MYSQL函数用法—
- Leave List-Processing 新解
- 关于Ext.onReady的调用方法
- setsockopt 设置socket 详细用法
- 让Visual Studio 也支持JS代码折叠 [ #region | #endregion ]
- arm-linux-gcc 裸机程序开发(二)