简体   繁体   中英

Multiple SQL table Join-

I have three tables Installation,Offices,Branches.

Installation - 
ID | Date | Status | Reference_by

Offices - 
Installation ID | Branch ID

Branches - 
Branch ID | Branch Name

I want to display a table showing

Branch Name| Successful Installation(i.e status=installed)

where branch name column shows all the list of branches and successful installation shows corresponding bracnches installation count..

Try this query:

select B.BranchName, count(I.Status) as 'Successful Installation'
from Branches B inner join Offices O on B.BranchId = O.BranchId
inner join Installation I on O.InstallationId = I.Id
 where I.Status = 'installed' --(use this clause for a particular condition on Status)
group by B.BrId, B.BranchName

Probably a good idea to read up on SQL joins and grouping( http://www.w3schools.com/sql/sql_join.asp ) if you plan on continuing with development in SQL.

First remove white space from column name. alter structure with, Installation - ID | Date | Status | Reference_by Offices - I_ID | B_ID Branches - B_ID | B_Name

Now you can use this query

select b.B_Name,count(i.Status) from Installation i 
join Offices o on i.I_Id = o.I_ID  
join Branches b on b.B_ID=o.B_ID 
where i.Status ='installed' group by b.B_Name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM