如何合并具有相同列名的两个表

How to merge two tables with having same column name

select *
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty 
order by 1, 6

我收到以下错误:

Column names in each table must be unique. Column name 'Specialty' in table 'aTable' is specified more than once.

bt table 的列:

Location
Specialty
Provider

btt 的列 table:

Specialty
Topic

我正在尝试获取位置、专业、提供者和(加入专业的所有主题)。

你可以这样做:

select btg.*, topic
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty 

这里的问题是 "Select *",它将 select 您结果集中的所有字段。尝试指定要插入的特定字段。

例如:

SELECT Location, btg.Specialty, Provider, Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty 
order by 1, 6

您只需要提及别名即可避免在连接具有相同列名的两个表时发生冲突。

SELECT btg.Location, btg.Specialty, btg.Provider,btg.Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg join dbo.btt bta on btg.specialty = bta.specialty 
order by 1, 6