如何编写一个返回所有表中所有站点ID的SQL脚本:
此图片中的表格:
这就是我想要的回复:
...因为只有网站1&所有四个表都有2个。
数据库:SQL Azure(但我认为不重要)
答案 0 :(得分:2)
您可以使用join
:
select t1.id
from table1 t1 join
table2 t2
on t1.id = t2.id join
table3 t3
on t1.id = t3.id join
table4 t4
on t1.id = t4.id;
答案 1 :(得分:2)
select SiteID
from table1 t1
join table2 t2 on t1.SiteID = t2.SiteID
join table3 t3 on t2.SiteID = t3.SiteID
join table4 t4 on t3.SiteID = t4.SiteID
join table5 t5 on t4.SiteID = t5.SiteID;
答案 2 :(得分:0)
我不确定这对所有数据库引擎是否相同,但在postgres
中你可以这样做。
select table1.siteid from table1,table2,table3 where (table1.siteid=table2.siteid) and (table1.siteid=table3.siteid);
答案 3 :(得分:0)
是的,SQL Server支持INTERSECT运算符。这是最简单的写作方式:
从table1中选择SiteID 相交 从table2中选择SiteID 相交 从table3 ....中选择SiteID。
这将转换为适当的连接。