我有 4 个表,其中一列在所有表中都是通用的。有没有一种方法可以创建一个 View ,在该 View 中我可以通过同一列连接所有表,而我只看到一次公共(public)列。
假设我有 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
这是我用来连接表的代码;
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
,如下所示;
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 多栏,所以很难全部写完。
抱歉,如果这是一个非常愚蠢的问题,我已经检查过以前的类似问题,但无法弄清楚,感谢您的帮助。
最佳答案
您在所有表上都有公共(public)列,因此可以使用 using(common_column)
删除重复的列。
SELECT *
FROM table1
LEFT JOIN table2 using(Cust_ID)
LEFT JOIN table3 using(Cust_ID)
LEFT JOIN table4 using(Cust_ID)
我希望这很有用。
关于SQL Server : removing duplicate column while joining tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41928158/