2个表a表
a1 a2b表
b1 b2a2和b2是一样的,怎么从b表取b1到a1写了2种语句update a
set a1=b.b1
where b.b2=a.a2
报错:列前缀'b'与查询中所用的表名或别名不匹配update a
set a1=(select b1 from a,b where b.b2=a.a2)
报错:子查询返回的值多于一个真的迷糊了,还请各位DX帮忙看下。
a1 a2b表
b1 b2a2和b2是一样的,怎么从b表取b1到a1写了2种语句update a
set a1=b.b1
where b.b2=a.a2
报错:列前缀'b'与查询中所用的表名或别名不匹配update a
set a1=(select b1 from a,b where b.b2=a.a2)
报错:子查询返回的值多于一个真的迷糊了,还请各位DX帮忙看下。
set a1=b.b1
where b.b2=a.a2
改為
update a
set a1=b.b1
from a, b
where b.b2=a.a2
update a
set a1=(select b1 from a,b where b.b2=a.a2)
改為
update a
set a1=(select Max(b1) from b where b2=a.a2)
set a1=b.b1
from a,b
where b.b2=a.a2update a
set a1=(select top 1 b1 from b where b2=a.a2)
set a1=b.b1
from b
inner join a
on b.b2=a.a2update a
set a1=(select b1 from a,b where b.b2=a.a2)--查询返回的b1不止一个
(a1 Int,
a2 Int)
Insert a Select 0, 1
Union All Select 0, 2Create Table b
(b1 Int,
b2 Int)
Insert b Select 11, 1
Union All Select 22, 2
GO
update a
set a1=b.b1
from a, b
where b.b2=a.a2Select * From a
GO
Drop Table a, b
--Result
/*
a1 a2
11 1
22 2
*/
(a1 Int,
a2 Int)
Insert a Select 0, 1
Union All Select 0, 2Create Table b
(b1 Int,
b2 Int)
Insert b Select 11, 1
Union All Select 22, 2
GO
update a
set a1=(select Max(b1) from b where b2=a.a2)Select * From a
GO
Drop Table a, b
--Result
/*
a1 a2
11 1
22 2
*/
set a1=(select Max(b1) from b where b2=a.a2)
set a1=b.b1
where b.b2=a.a2
报错:列前缀'b'与查询中所用的表名或别名不匹配--------->对应的select 语句是
select * from a where b.b2=a.a2 这当然有错了.好的建议:
在update之前,最好先select.
update a
set a1=(select Max(b1) from b where b2=a.a2)
----------
怎麼老是復制?
一直都是单表做UPDATE,多表很少做。
又学到了,呵呵。
谢谢了,我用的是第1个语句。
一直都是单表做UPDATE,多表很少做。
又学到了,呵呵。
----------
用第一個的效率更優些。 :)