MySQL数据库update更新子查询

发表于:2016-7-22 10:08

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:跳动的汗水    来源:51Testing软件测试网采编

#
MySQL
分享:
  比如:
  UPDATE test.tb_vobile a
  set a.name = '111 '
  WHERE
  a.id = (select max(id) id from test.tb_vobile)
  报错:
  [SQL]UPDATE test.tb_vobile a
  set a.name = '111 '
  WHERE
  a.id = (select max(id) id from test.tb_vobile)
  以下可通过:
  UPDATE test.tb_vobile a
  join
  (select max(id) id from test.tb_vobile) b
  on a.id = b.id
  set a.name = '123 ';
  或
  UPDATE test.tb_vobile a ,(select max(id) id from test.tb_vobile) b
  set a.name = '321 '
  WHERE
  a.id = b.id ;
  说明:
  1、update 时,更新的表不能在set和where中用于子查询;
  2、update 时,可以对多个表进行更新(sqlserver不行);
  如:update ta a,tb b set a.Bid=b.id ,b.Aid=a.id;
  3、update 后面可以做任意的查询,这个作用等同于from;
  UPDATE test.tb_vobile a
  set a.name = '111 '
  WHERE
  a.id = (select max(id) id from test.tb_vobile)
  报错:
  [SQL]UPDATE test.tb_vobile a
  set a.name = '111 '
  WHERE
  a.id = (select max(id) id from test.tb_vobile)
  [Err] 1093 - You can't specify target table 'a' for update in FROM clause
  以下可通过:
  UPDATE test.tb_vobile a
  join
  (select max(id) id from test.tb_vobile) b
  on a.id = b.id
  set a.name = '123 ';
  或
  UPDATE test.tb_vobile a ,(select max(id) id from test.tb_vobile) b
  set a.name = '321 '
  WHERE
  a.id = b.id ;
  说明:
  1、update 时,更新的表不能在set和where中用于子查询;
  2、update 时,可以对多个表进行更新(sqlserver不行);
  如:update ta a,tb b set a.Bid=b.id ,b.Aid=a.id;
  3、update 后面可以做任意的查询,这个作用等同于from;
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号