SQL一次更新多条数据,这个更新的sql语句为什么有错?

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:53:00
我要一次更新tableA的多条数据,但是我的这个语句有错
update tableA a set a.id=(select b.id from tableB b where a.name=b.name)
请各位高手指点一下
请给出正确的写法 谢谢

select b.id from tableB b where a.name=b.name)
出来的很可能不止一个值
那么你这样
a.id=(select b.id from tableB b where a.name=b.name)
就错了
我要一次更新tableA的多条数据 你想把a.id改成什么值呢?
update tableA a set a.id=你想改的值

用 a.name=b.name 能唯一确定一个 b.id么?
如果 select b.id from tableB b where a.name=b.name
的结果是多条记录,那么sql 无法将一个结果集赋值给一个字段。
比如 select b.id from tableB b ,tableA a where a.name=b.name and a.name= '小李'
如果得到1个结果 :1
那么你可以将a 表中小李这一行对应的id值 设为1
如果得到的是一个结果集(10个值):
1
2
3
4
5
6
7
8
9
10
这样的话,你将a 表中小李这一行对应的id值 置为什么呢?

请检查你的where 条件,使得确定一个 唯一 的b.id

语句没错,但是你要保证tableB 中的name字段的值没有相同的,如不能有两个人都叫‘张三’
否则
select b.id from tableB b where a.name=b.name的值就可能有多个
无法作为和a.id比较的条件

update tableA as a set id=(select id from tableB as b where
b.name=a.name)

如果数据比较少的话: 不妨用这个 update tableA set id=(select id from tableB where name='003')where name='003'