移动文章分类的sql语句,查询并更新同一个表的方法 You can't specify target table 'news_articles' for update in FROM clause
来源:互联网 发布:淘宝法院拍卖房产网站 编辑:程序博客网 时间:2024/06/05 17:03
移动文章分类的sql语句,很简单的一个
要把公司新闻的一些文章移动到公司新闻分类去。手动改麻烦,写个SQL直接改。
行业新闻分类的ID是19
公司新闻分类的ID是20
SQL代码如下:
UPDATE `news_articles` SET `category_id` = 20 where `article_id` in (SELECT distinct n.`article_id` FROM `news_articles` as n, `news_articles_text` as nt WHERE n.article_id = nt.article_id and nt.news_article_name like '%Wision%'and n.category_id = '19' )UPDATE `news_articles` SET `category_id` = 20 where `article_id` in (42,44,46,48,49,50,51,53,54,55,56,67,76,77,78,79,80,81,83,84,85,86,87,88,89,90,91,92,93,97,98,99,100,122)
我执行第一条的时候提示:
#1093 - You can't specify target table 'news_articles' for update in FROM clause
在网上查询了一下资料:
解决方法:sql改正如下可以正确执行:
UPDATE `news_articles` SET `category_id` = 20 where `article_id` in (select a.`article_id` from ( select distinct n.`article_id` FROM `news_articles` as n, `news_articles_text` as nt WHERE n.article_id = nt.article_id and nt.news_article_name like '%Wision%'and n.category_id = '19') as a )
格式化的sql语句
UPDATE `news_articles` SET `category_id` =20 WHERE `article_id` IN (SELECT a.`article_id`FROM (SELECT DISTINCT n.`article_id`FROM `news_articles` AS n, `news_articles_text` AS ntWHERE n.article_id = nt.article_idAND nt.news_article_name LIKE '%Wision%'AND n.category_id = '19') AS a)
关键是子查询
下面是查询到的资料,不清楚的看下面的资料。
=================
今天在写 mysql 遇到一个比较特殊的问题。
mysql 语句如下:
update 表名 set cabf_enabled=0
where cabf_id in (
SELECT 表名.cabf_id FROM 表名
Inner Join wms_cabinet ON 表名.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 表名.cabf_enabled=1)
运行时提出如下提示: You can't specify target table '表名' for update in FROM clause
运行 in 里面的 select 字句:
?
SELECT 表名.cabf_id FROM 表名
Inner Join wms_cabinet ON 表名.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 表名.cabf_enabled=1
?
可以正确 select 正确结果。再把结果直接写到 in 里面,改后语句如下:
update 表名 set cabf_enabled=0 where cabf_id in ('113','114','115'),再运行可以正确执行更新。
到这一步开始想不明白,为什么用 select 子句运行会出错呢?以前在 mssql 这种写法是很常见的。
没办法了,唯有动用 baidu。找到两条记录。
原来原因是:mysql中不能这么用。 (等待mysql升级吧)。那串英文错误提示就是说,不能先select出同一表中的某些值,
再update这个表(在同一语句中)。 也找到替代方案,重写改写了 sql 。
?
改写后的 sql 如下所示,大家仔细区别一下。
?
update 表名 set cabf_enabled=0 where cabf_id in (
SELECT a.cabf_id FROM (select tmp.* from 表名 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 表名 tmp) a ,我 select tmp.* from 表名 tmp 作为子集,
然后再 select a.cabf_id FROM 子集,这样就不会 select 和 update 都是同一个表。致此问题得到完美解决。
=============================
- 移动文章分类的sql语句,查询并更新同一个表的方法 You can't specify target table 'news_articles' for update in FROM clause
- mysql SQL语句You can't specify target table ' for update in FROM clause解决
- mysql执行SQL语句时,出现You can't specify target table '表名' for update in FROM clause错误
- 关于mysql "You can't specify target table 'test_zb_scene' for update in FROM clause"的错误
- mysql 出现You can't specify target table for update in FROM clause错误的解决方法
- mysql错误:1093-You can’t specify target table for update in FROM clause的解决方法
- mysql update语句问题:You can't specify target table 'user' for update in FROM clause
- MySQL数据库update更新子查询[Err] 1093 - You can't specify target table 'text' for update in FROM clause
- 做leetcode的sql题遇到:You can't specify target table 'Person1' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table '表名' for update in FROM clause
- MYSQL 临时表 #1093 - You can't specify target table 'dealer_admin' for update in FROM clause
- MYSQL问题:You can't specify target table '表名' for update in FROM clause
- mysql的1093错误You can't specify target table 't' for update in FROM clause 简单解决
- 【mysql 】sql删除重复记录 You can't specify target table '表名' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql中错误:1093-You can’t specify target table for update in FROM clause的解决方法
- C语言预处理功能 关于字符串化和符号粘贴
- Linux-mail的邮件收发及察看
- wordpress 301重定向 htaccess
- 内核里面writel(readl)是如何实现的
- DX11渲染引擎进展
- 移动文章分类的sql语句,查询并更新同一个表的方法 You can't specify target table 'news_articles' for update in FROM clause
- 嘟嘟才能用。。
- IT职场人生系列之二十三:知识体系(专家与杂家)
- HTML中css样式简介
- iOS 静态类库项目的建立与使用
- AWS云平台系列介绍(一):AWS平台与EC2介绍
- Java背后的秘密之Java的类装载器03
- ARM系统中断产生流程
- 最大权闭合图