在sql中将表连接在一起 [英] Join tables together in sql

查看:97
本文介绍了在sql中将表连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试编写一个查询以匹配两个表中的colbus_name,如果匹配则将表2中的收入复制到table1 。

表1 :( - 表示空间)

Bus_id - bus_name - Bus_address - 收入

1010 - Tata motr- -Coll north-- NULL

1020 - Celeriac - Celeriac new-- NULL

1030 - 泰戈尔 - 泰戈尔南 - NULL

1040 - 尼赫鲁 - 尼赫鲁王 - 空白



表2:

New_id - bus_name - Bus_address - 收入

20 - 全新 - westgate new-- 75050

10 - Tata motr - 北 - 10050

30--玻璃 - 玻璃 - 80900

40 - 泰戈尔 - 泰戈尔南部 - 25000



结果表t1应为:

Bus_id - bus_name - Bus_address - 收入

1010 - Tata motr - Coll north-- 10050

1020 - Celeriac-- Celeriac new-- NULL

1030 - 泰戈尔 - 泰戈尔南部 - 25000

1040 - 尼赫鲁 - 尼赫鲁 - 诺瓦



我写过qu如下所示,但它不起作用:



更新t1

设置t1.Revenue = t2.revenue

什么时候(

select * from t1

Inner Join t2

on t1.r1_bus_name_full = t2.r1_bus_name_full);



请帮助!!

Hi,

I am trying to write a query to match a col "bus_name" in two table and if it matches copy the revenue from table 2 to table1.
Table 1 : (-- means space)
Bus_id--bus_name--Bus_address--revenue
1010--Tata motr--Coll north-- NULL
1020--Celeriac--Celeriac new-- NULL
1030--Tagore--Tagore south-- NULL
1040--Nehru--Nehru King-- NULL

Table 2 :
New_id--bus_name--Bus_address--revenue
20--Class new--westgate new-- 75050
10--Tata motr--north-- 10050
30--Glass--Glass-- 80900
40--Tagore--Tagore south-- 25000

Resulting table t1 should be:
Bus_id--bus_name--Bus_address--revenue
1010--Tata motr--Coll north-- 10050
1020--Celeriac--Celeriac new-- NULL
1030--Tagore--Tagore south-- 25000
1040--Nehru--Nehru King-- NULL

I have written query as below but it doesnt work :

Update t1
set t1.Revenue = t2.revenue
when (
select * from t1
Inner Join t2
on t1.r1_bus_name_full = t2.r1_bus_name_full ) ;

Pls help !!

推荐答案

请更改您的查询并尝试:

Please change your query like this and try:
update a set a.revenue = b.revenue
from table1 a inner join table2 b 
on a.busname = b.busname



你也可以使用MERGE实现相同:


You can also use MERGE to achieve the same:

merge table1 as a
using table2 as b
on (a.busname = b.busname)
when matched
	then update set a.revenue = b.revenue;





注意:列名可能不匹配,但你明白了。



Note: Column names may not match, but you get the idea.


这篇关于在sql中将表连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆