SQL Server:联接表时删除重复的列 [英] SQL Server : removing duplicate column while joining tables
问题描述
我有4个表,其中一列在所有表上都通用.有没有一种方法可以创建一个视图,在该视图中我可以按同一列连接所有表,而我只能看到一次公共列.
I have 4 tables with one column is common on all tables. Is there a way to create a view where I can join all tables by same column where I see the common column only once.
假设我有table1
Let's say I have table1
Cust ID | Order ID | Product_Name
表2
Cust_ID | Cust_Name | Cust_Address
表3
Cust_ID | Cust_Acc | Acc_Type
表4
Cust_ID | Contact_Phone | Cust_Total_Ord
这是我用来联接表的代码;
Here is the code I use to join tables;
SELECT *
FROM table1
LEFT JOIN table2 ON table1.Cust_ID = table2.Cust_ID
LEFT JOIN table3 ON table2.Cust_ID = table3.Cust_ID
LEFT JOIN table4 ON table3.Cust_ID = table4.Cust_ID
我将所有表连接在一起,从每个表中看到Cust_ID
,如下所示;
I get all tables joined by I see Cust_ID
from each table as below;
Cust ID| Order ID|Product_Name| Cust_ID| Cust_Name|Cust_Address| Cust_ID| Cust_Acc| Acc_Type|Cust_ID|Contact_Phone|Cust_Total_Ord
是否可以删除重复的Cust_ID
列,还是需要在SELECT
中写入每个列的名称?我总共有50列以上,因此很难全部编写.
Is there a way to remove duplicate Cust_ID
columns or do I need to write each column name in the SELECT
? I have more than 50 columns in total so will be difficult to write all.
对不起,如果这是一个非常愚蠢的问题,我已经检查了以前的类似问题,但无法弄清楚,谢谢您的帮助.
Sorry if it is a really dumb question, I have checked previous similar questions but couldn't figure out and thanks for help.
推荐答案
您在所有表上都有公共列,因此可以使用using(common_column)
删除重复的列.
your have common columns on all tables so could use using(common_column)
to remove duplicated columns.
SELECT *
FROM table1
LEFT JOIN table2 using(Cust_ID)
LEFT JOIN table3 using(Cust_ID)
LEFT JOIN table4 using(Cust_ID)
我希望那很有用.
这篇关于SQL Server:联接表时删除重复的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!