2023年8月3日发(作者:)
mysqlupdate不⽀持⼦查询更新先看⽰例:SELECT uin,account,password,create_user_uin_tree FROM sys_user结果:表中的create_user_uin_tree标识该条记录由谁创建。创建新⽤户时,根据当前登录⽤户的uin及新创建的⽤户uin,有如下SQL:select concat(ifNULL(create_user_uin_tree,concat('_',2,'_')),'|_','97',"_") from sys_user where uin=2结果:那么修改的create_user_uin_tree的标识SQL为:update sys_user set create_user_uin_tree=(select concat(ifNULL(_user_uin_tree,concat('_',2,'_')),'|_','97',"_") from sys_user temp where =2) where uin = 97;报错信息:Error Code: 1093. You can't specify target table 'sys_user' for update in FROM clause 0.000 sec原因:mysql中不⽀持⼦查询更新,准确的说是更新的表不能在set和where中⽤于⼦查询。那串英⽂错误提⽰就是说,不能先select出同⼀表中的某些值,再update这个表(在同⼀语句中)。
两种⽅法:1、select from 修改为⼦查询⽅式2、inner join⽅法1:⼦查询⽅式调整了下SQL:update sys_user set
create_user_uin_tree=(select from (select concat(ifNULL(create_user_uin_tree,concat('_',97,'_')),'|_','98',"_") as tree from sys_user where uin=97) temp) where uin = 98;我将作为⼦集,select concat(ifNULL(create_user_uin_tree,concat('_',97,'_')),'|_','98',"_") as tree from sys_user where uin=97) temp然后再select from(⼦集) ⼦集,这样就不会 select 和 update 都是同⼀个表。致此问题得到完美解决。
⽅法2:inner join参考:在sql server中,我们可是使⽤以下update语句对表进⾏更新:update a set = (select yy from b) where = ;但是在mysql中,不能直接使⽤set select的结果,必须使⽤inner join:update a inner join (select yy from b) c on = set = L不允许SELECT FROM后⾯指向⽤作UPDATE的表,有时候让⼈纠结。当然,有⽐创建⽆休⽌的临时表更好的办法。本⽂解释如何UPDATE⼀张表,同时在查询⼦句中使⽤SELECT.假设我要UPDATE的表跟查询⼦句是同⼀张表,这样做有许多种原因,例如⽤统计数据更新表的字段(此时需要⽤group⼦句返回统计值),从某⼀条记录的字段update另⼀条记录,⽽不必使⽤⾮标准的语句,等等。举个例⼦:错误提⽰是:ERROR 1093 (HY000): You can't specify target table 'apples' for update in FROM clause. MySQL⼿册这下⾯有说明 : “Currently,you cannot update a table and select from the same table in a subquery.”在这个例⼦中,要解决问题也⼗分简单,但有时候不得不通过查询⼦句来update⽬标。好在我们有办法。既然MySQL是通过临时表来实现FROM⼦句⾥⾯的嵌套查询,那么把嵌套查询装进另外⼀个嵌套查询⾥,可使FROM⼦句查询和保存都是在临时表⾥进⾏,然后间接地在外围查询被引⽤。下⾯的语句是正确的:CREATE TABLE `user2` ( `id` INT(11) NOT NULL, `name` VARCHAR(50) DEFAULT NULL, `device` VARCHAR(40) DEFAULT NULL, `memo` VARCHAR(40) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO `user2`(`id`,`name`,`device`,`memo`) VALUES (1,'duan','11',NULL),(2,'liang','12',NULL),(3,'hou','13',NULL);//⼦查询UPDATE user2 SET memo=(SELECT FROM (SELECT device AS dc FROM user2 WHERE id=2) temp) WHERE id = 2;//列=列UPDATE user2 SET memo=device;//join如果你想了解更多其中的机制,请阅读相关章节。没有解决的问题⼀个常见的问题是,IN()⼦句优化废品,被重写成相关的嵌套查询,有时(往往?)造成性能低下。把嵌套查询装进另外⼀个嵌套查询⾥并不能阻⽌它重写成相关嵌套,除⾮我下狠招。这种情况下,最好⽤JOIN重构查询()。另⼀个没解决的问题是临时表被引⽤多次。“装进嵌套查询”的技巧⽆法解决这些问题,因为它们在编译时被创建,⽽上⾯讨论的update问题是在运⾏时。
另外⼀个⽰例:交换两条记录中的字段值⽅法表test:
id priority
1 1
2 2
⽅法⼀:mysql语句如下:update question set sort=(case when id=7 then (select from (select tmp.* from question tmp) awhere =8) when id=8 then (select from (select tmp.* from question tmp) a where =7) end)where id=7 or id=8;
⽅法⼆:update question as q1 join question as q2 on (=7 and = 8)or( = 8 and =7)set = ,=;
2023年8月3日发(作者:)
mysqlupdate不⽀持⼦查询更新先看⽰例:SELECT uin,account,password,create_user_uin_tree FROM sys_user结果:表中的create_user_uin_tree标识该条记录由谁创建。创建新⽤户时,根据当前登录⽤户的uin及新创建的⽤户uin,有如下SQL:select concat(ifNULL(create_user_uin_tree,concat('_',2,'_')),'|_','97',"_") from sys_user where uin=2结果:那么修改的create_user_uin_tree的标识SQL为:update sys_user set create_user_uin_tree=(select concat(ifNULL(_user_uin_tree,concat('_',2,'_')),'|_','97',"_") from sys_user temp where =2) where uin = 97;报错信息:Error Code: 1093. You can't specify target table 'sys_user' for update in FROM clause 0.000 sec原因:mysql中不⽀持⼦查询更新,准确的说是更新的表不能在set和where中⽤于⼦查询。那串英⽂错误提⽰就是说,不能先select出同⼀表中的某些值,再update这个表(在同⼀语句中)。
两种⽅法:1、select from 修改为⼦查询⽅式2、inner join⽅法1:⼦查询⽅式调整了下SQL:update sys_user set
create_user_uin_tree=(select from (select concat(ifNULL(create_user_uin_tree,concat('_',97,'_')),'|_','98',"_") as tree from sys_user where uin=97) temp) where uin = 98;我将作为⼦集,select concat(ifNULL(create_user_uin_tree,concat('_',97,'_')),'|_','98',"_") as tree from sys_user where uin=97) temp然后再select from(⼦集) ⼦集,这样就不会 select 和 update 都是同⼀个表。致此问题得到完美解决。
⽅法2:inner join参考:在sql server中,我们可是使⽤以下update语句对表进⾏更新:update a set = (select yy from b) where = ;但是在mysql中,不能直接使⽤set select的结果,必须使⽤inner join:update a inner join (select yy from b) c on = set = L不允许SELECT FROM后⾯指向⽤作UPDATE的表,有时候让⼈纠结。当然,有⽐创建⽆休⽌的临时表更好的办法。本⽂解释如何UPDATE⼀张表,同时在查询⼦句中使⽤SELECT.假设我要UPDATE的表跟查询⼦句是同⼀张表,这样做有许多种原因,例如⽤统计数据更新表的字段(此时需要⽤group⼦句返回统计值),从某⼀条记录的字段update另⼀条记录,⽽不必使⽤⾮标准的语句,等等。举个例⼦:错误提⽰是:ERROR 1093 (HY000): You can't specify target table 'apples' for update in FROM clause. MySQL⼿册这下⾯有说明 : “Currently,you cannot update a table and select from the same table in a subquery.”在这个例⼦中,要解决问题也⼗分简单,但有时候不得不通过查询⼦句来update⽬标。好在我们有办法。既然MySQL是通过临时表来实现FROM⼦句⾥⾯的嵌套查询,那么把嵌套查询装进另外⼀个嵌套查询⾥,可使FROM⼦句查询和保存都是在临时表⾥进⾏,然后间接地在外围查询被引⽤。下⾯的语句是正确的:CREATE TABLE `user2` ( `id` INT(11) NOT NULL, `name` VARCHAR(50) DEFAULT NULL, `device` VARCHAR(40) DEFAULT NULL, `memo` VARCHAR(40) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO `user2`(`id`,`name`,`device`,`memo`) VALUES (1,'duan','11',NULL),(2,'liang','12',NULL),(3,'hou','13',NULL);//⼦查询UPDATE user2 SET memo=(SELECT FROM (SELECT device AS dc FROM user2 WHERE id=2) temp) WHERE id = 2;//列=列UPDATE user2 SET memo=device;//join如果你想了解更多其中的机制,请阅读相关章节。没有解决的问题⼀个常见的问题是,IN()⼦句优化废品,被重写成相关的嵌套查询,有时(往往?)造成性能低下。把嵌套查询装进另外⼀个嵌套查询⾥并不能阻⽌它重写成相关嵌套,除⾮我下狠招。这种情况下,最好⽤JOIN重构查询()。另⼀个没解决的问题是临时表被引⽤多次。“装进嵌套查询”的技巧⽆法解决这些问题,因为它们在编译时被创建,⽽上⾯讨论的update问题是在运⾏时。
另外⼀个⽰例:交换两条记录中的字段值⽅法表test:
id priority
1 1
2 2
⽅法⼀:mysql语句如下:update question set sort=(case when id=7 then (select from (select tmp.* from question tmp) awhere =8) when id=8 then (select from (select tmp.* from question tmp) a where =7) end)where id=7 or id=8;
⽅法⼆:update question as q1 join question as q2 on (=7 and = 8)or( = 8 and =7)set = ,=;
发布评论