yxhxj2006

常用链接

统计

最新评论

如何连接表更新数据

sql server中如何连接表更新数据
假设a,b两表有外键关联,想要从b表中取出相应字段的值更新a表字段,可以有如下几种写法:
update a set a.name=b.name from a,b where a.id=b.id 
update a inner join b on a.id=b.id set a.name=b.name where ... 
update table1 set a.name = b.name from table1 a inner join table2 b on a.id =b


二:

表hotel中有id,hotel_city。

    表hotel_room中有id,hotel_id,其中hotel_id与表hotel中的id对应。

    表hotel_room_price中有room_id,price,price_year_month,其中room_id与hotel_room中的id对应。其余各项数据之间没有关联。

    现在要求更新表hotel_room_price中的price字段,符合的条件是room_id对应的hotel_city是744,且price_year_month是'20114'。

    SQL语句:

    update hotel_room_price set price = 0 from hotel_room_price a,hotel_room b,hotel c where a.room_id = b.id and b.hotel_id = c.id and c.hotel_city = 744 and a.price_year_month = '20114'

    重点看红色的部分。from后跟的是需要联立的三个表。用where a.room_id = b.id and b.hotel_id = c.id来将三个表当中需要对应的字段联立起来。and c.hotel_city = 744 and a.price_year_month = '20114'则是最后指定的两个条件。

    运行无错误。

posted on 2013-11-01 01:51 奋斗成就男人 阅读(171) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: