SQL 许多加入多对多
SQL many joins many to many
我有 5 个table:
Table 报告
Report_Id | Report_name
-----------------
1 | Income
2 | Outcomes
3 | Costs
多对多 table ReportsIpRel
Ip_Id | Report_Id
-----------------
6 | 1
4 | 2
5 | 2
2 | 2
1 | 3
Table 个信息提供者
Ip_Id | Ip_Name
-----------------
6 | Comission
4 | Comapny
5 | Others
2 | People
1 | Traveler
多对多 table QueriesIpRel
Ip_Id | Query_Id
-----------------
6 | 3
4 | 3
5 | 3
2 | 5
1 | 1
和 table 的查询
Query_Id | Query_Name
-----------------
1 | connection
2 | distantcon
3 | shortconn
4 | linking
5 | grounding
我想要实现的SELECT如下:
Report_Id | Report_name | Ip_Id | Ip_Name | Query_Id | Query_Name
2 | Outcomes | 4 | Comapny | 3 | shortconn
2 | Outcomes | 5 | Others | 3 | shortconn
2 | Outcomes | 2 | People | 5 | grounding
我尝试了很多 left/inner 这样的连接:
left outer join ReportsIPRel rir on r.Report_Id = rir.Report_Id
left outer join InfoProvider ipr on rir.Ip_Id = ipr.Ip_Id
left outer join QueriesIPRel qir on ipr.Ip_Id = qir.Ip_Id
left outer join Queries q on qir.Query_Id = q.Query_Id
where r.Report_Id= '2'
但一切都是徒劳的。
我必须使用 where 条件:WHERE report_Id = '2' /EXAMPLE
我正尝试在 SQL Server Management Studio 中以 ms SQL 执行此操作。
提前致谢
请在 SQL 服务器中尝试此查询对我有效:
select r.*, ipr.ip_id, ipr.Ip_Name, q.uery_Id, q.Query_Name from
reports r
left outer join ReportsIPRel rir on r.Report_Id = rir.Report_Id
left outer join InfoProviders ipr on rir.Ip_Id = ipr.Ip_Id
left outer join QueriesIPRel qir on ipr.Ip_Id = qir.Ip_Id
left outer join Queries q on qir.Query_Id = q.uery_Id
where r.Report_Id= '2'
我有 5 个table:
Table 报告
Report_Id | Report_name
-----------------
1 | Income
2 | Outcomes
3 | Costs
多对多 table ReportsIpRel
Ip_Id | Report_Id
-----------------
6 | 1
4 | 2
5 | 2
2 | 2
1 | 3
Table 个信息提供者
Ip_Id | Ip_Name
-----------------
6 | Comission
4 | Comapny
5 | Others
2 | People
1 | Traveler
多对多 table QueriesIpRel
Ip_Id | Query_Id
-----------------
6 | 3
4 | 3
5 | 3
2 | 5
1 | 1
和 table 的查询
Query_Id | Query_Name
-----------------
1 | connection
2 | distantcon
3 | shortconn
4 | linking
5 | grounding
我想要实现的SELECT如下:
Report_Id | Report_name | Ip_Id | Ip_Name | Query_Id | Query_Name
2 | Outcomes | 4 | Comapny | 3 | shortconn
2 | Outcomes | 5 | Others | 3 | shortconn
2 | Outcomes | 2 | People | 5 | grounding
我尝试了很多 left/inner 这样的连接:
left outer join ReportsIPRel rir on r.Report_Id = rir.Report_Id
left outer join InfoProvider ipr on rir.Ip_Id = ipr.Ip_Id
left outer join QueriesIPRel qir on ipr.Ip_Id = qir.Ip_Id
left outer join Queries q on qir.Query_Id = q.Query_Id
where r.Report_Id= '2'
但一切都是徒劳的。
我必须使用 where 条件:WHERE report_Id = '2' /EXAMPLE
我正尝试在 SQL Server Management Studio 中以 ms SQL 执行此操作。
提前致谢
请在 SQL 服务器中尝试此查询对我有效:
select r.*, ipr.ip_id, ipr.Ip_Name, q.uery_Id, q.Query_Name from
reports r
left outer join ReportsIPRel rir on r.Report_Id = rir.Report_Id
left outer join InfoProviders ipr on rir.Ip_Id = ipr.Ip_Id
left outer join QueriesIPRel qir on ipr.Ip_Id = qir.Ip_Id
left outer join Queries q on qir.Query_Id = q.uery_Id
where r.Report_Id= '2'