在 sql 中使用 join on 后删除重复的列
Remove duplicate columns after using join on in sql
我有以下 sql 查询,它在两列上连接两个 select 语句
SELECT * FROM
(SELECT TOP(100)
Name
,ItemNum
,TicketNum
FROM [dbo].[dd]
) t1
JOIN
(SELECT TOP (100)
TicketNum
,ItemNum
FROM [dbo].[dd]) t2
ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum
查询产生以下输出。
|名称 | ItemNum |TicketNum |TicketNum |ItemNum |
但我希望输出是
|名称 | ItemNum |TicketNum |
解决方案一:
像这样指定列名
SELECT
t1.Name, t1.ItemNum, t1.TicketNum
FROM (
SELECT TOP 100
Name, ItemNum, TicketNum
FROM [dbo].[dd]
) t1
INNER JOIN (
SELECT TOP 100
TicketNum, ItemNum
FROM [dbo].[dd]
) t2 ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum
方案二:
SELECT t1.* FROM (
SELECT TOP 100
Name, ItemNum, TicketNum
FROM [dbo].[dd]
) t1
INNER JOIN (
SELECT TOP 100
TicketNum, ItemNum
FROM [dbo].[dd]
) t2 ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum
我有以下 sql 查询,它在两列上连接两个 select 语句
SELECT * FROM
(SELECT TOP(100)
Name
,ItemNum
,TicketNum
FROM [dbo].[dd]
) t1
JOIN
(SELECT TOP (100)
TicketNum
,ItemNum
FROM [dbo].[dd]) t2
ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum
查询产生以下输出。
|名称 | ItemNum |TicketNum |TicketNum |ItemNum |
但我希望输出是 |名称 | ItemNum |TicketNum |
解决方案一: 像这样指定列名
SELECT
t1.Name, t1.ItemNum, t1.TicketNum
FROM (
SELECT TOP 100
Name, ItemNum, TicketNum
FROM [dbo].[dd]
) t1
INNER JOIN (
SELECT TOP 100
TicketNum, ItemNum
FROM [dbo].[dd]
) t2 ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum
方案二:
SELECT t1.* FROM (
SELECT TOP 100
Name, ItemNum, TicketNum
FROM [dbo].[dd]
) t1
INNER JOIN (
SELECT TOP 100
TicketNum, ItemNum
FROM [dbo].[dd]
) t2 ON t1.ItemNum = t2.ItemNum
AND t1.TicketNum = t2.TicketNum