关于SQL的菜鸟问题
来源:百度知道 编辑:UC知道 时间:2024/06/26 02:22:28
现有A表,B表,字段:xh,xm,txdi,bz
提问如果A.xh=B.xh那么提取B.txdz到A.bz 是用select into 还是用update?
提问如果A.xh=B.xh那么提取B.txdz到A.bz 是用select into 还是用update?
update A set A.bz=(select B.txdz from B where A.xh=B.xh)
楼上的方法不严密,如果b.xh返回不止一条数据会出错
update A set A.bz=(select top 1 B.txdz from B where A.xh=B.xh)
加个top 1就没问题了
如果A.xh=B.xh 这个时候你应该知道 A和B表的记录都已经有了。又怎么会是insert into 呢?肯定是update了。
呵呵 貌似楼上的 也不怎么对啊 你直接把select top 1 B.txdz from B where A.xh=B.xh 第一条数据弄出来。
update后却把所有的A表的A.bz 都设置成了这个值。严重错误。
建议:可以做个函数循环update表A的数据。
update a set bz=b.txdz from a inner join b on a.xh=b.xh