【发布时间】:2021-02-09 16:51:47
【问题描述】:
我从 SQL 服务器开始。我加入了两张桌子。它们都包含一个名为“id”的列。
在内连接之后,我现在有两个具有相同名称(“id”)的列。
我该如何解决这个问题(- 无需输入我想保留的所有列。我有 200 列-)。
declare @contract_number nvarchar(255) = '2019.37080'
declare @reportingperiod nvarchar(255) = '2019-Q1'
select *
from ifrs17.output_bba
inner join ifrs17.unit_of_account
on unit_of_account.contract_number = output_bba.unit_of_account_number
and unit_of_account.reporting_period = output_bba.reporting_period
and unit_of_account.currency_source = output_bba.currency_source
where
output_bba.unit_of_account_number = @contract_number
and output_bba.meta_id in ( select max(meta_id) from ifrs17.output_bba where contract_number = @contract_number and legal_entity_code = 'SR' group by reporting_period )
and unit_of_account.meta_id in ( select max(meta_id) from ifrs17.unit_of_account where contract_number = @contract_number and legal_entity_code = 'SR' group by reporting_period )
and output_bba.time_index > 0
and output_bba.reporting_period = @reportingperiod
【问题讨论】:
-
你不能 - 所以开始打字吧。有一些工具和技术可以提供帮助,但这仍然取决于您使用“*”作为列列表。
-
方法很简单,列出你需要的列,不要用
*。列出你需要的列,你只会得到你需要的列。另外,我建议在您的查询中为您的对象提供别名;它会让你的 SQL 更加简洁。 -
我接受使用
SELECT *的唯一时间是 a) 同时仍在为您尝试获取 JOIN/WHERE 正确位置的查询进行原型设计,以及 b) 在 @ 987654325@测试。否则,使用您神话般的“除一列之外的所有内容”功能,当有人在您不需要的每一行中添加额外的 2GB 文档列时会发生什么。选择您实际需要的列,我怀疑所有 200 列都是相关的。 -
我不同意,@TheImpaler,Damien 给出了一个(非常好的)理由。应该不像
* EXCEPT SomeColumn这样的语法。我当然可以看到懒惰的人在VIEW的定义中使用这样的语法,那将是灾难性的。
标签: sql sql-server inner-join