I have 5 tables:
Table Reports
Report_Id | Report_name
-----------------
1 | Income
2 | Outcomes
3 | Costs
Many to many table ReportsIpRel
Ip_Id | Report_Id
-----------------
6 | 1
4 | 2
5 | 2
2 | 2
1 | 3
Table of InfoProviders
Ip_Id | Ip_Name
-----------------
6 | Comission
4 | Comapny
5 | Others
2 | People
1 | Traveler
Many to many table QueriesIpRel
Ip_Id | Query_Id
-----------------
6 | 3
4 | 3
5 | 3
2 | 5
1 | 1
And table of Queries
Query_Id | Query_Name
-----------------
1 | connection
2 | distantcon
3 | shortconn
4 | linking
5 | grounding
The SELECT I am trying to achieve is the following:
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
I tried many left/inner joins like this:
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'
but all for nothing.
I have to use where condition: WHERE report_Id = '2' /EXAMPLE
I'm trying to do this in ms SQL in SQL Server Management Studio.
Thank you in advance
Please try this query its working for me in SQL server:
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'