我有很多名称都相似的表(例如“ table1”,“ table2”,“ table3”等),我需要在查询中使用所有这些表。它们都包含它们加入的相同的两个变量(“ ID”和“日期”)。
至少有25种此类表,并且我具有对数据库的只读访问权限,因此我无法将它们组合在一起,甚至无法创建一个可以做到这一点的视图。
我的问题是:是否可以使用简单的快捷方式来连接所有这些表?如果是SAS,我将创建一个宏,但我使用的是Microsoft SQL Server Management Studio 2012。
不必这样做:
select *
from table1 a
join table2 b on a.id=b.id and a.date=b.date
join table3 c on b.id=c.id and b.date=c.date
join ....
join ....
join table25 y on x.id=y.id and x.date=y.date
我想做类似的事情:
select *
from merge(table1 - table25) using(id, date)
用适当的方法替换上面的“合并”语句。这样的事情可能吗?
正如评论中指出的那样,您要寻找的简洁语法不存在。
利用连接列都命名为相同的事实来缩短SQL的唯一方法是使用using
关键字:
select *
from table1 a
join table2 b using (id, date)
join table3 c using (id, date)
join ....
join ....
join table25 y using (id, date)
但是可悲的是,即使那样对您也不起作用,因为using
在SQL Server中无法识别该关键字。它确实可以在其他流行的数据库中工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句