Nimish Kulkarni
Nimish Kulkarni

Reputation: 59

SQL Nested Query using 3 tables

I have three tables Member, Branch, Mem_Branch. And i want to nest those tables. In mem_branch branchid and memberid is (foreignkey) and Branchname alongwith branchid is in Branch table and Memberid is in Member Table. Now i have a example suppose in mem_branch i have a memberid number 2 and his branchid is 1 and i want to display the branchname from Branch table whose branchid is 1 which relates to that memberid 2. How is it possible ?

Upvotes: 1

Views: 197

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21776

SELECT Branch.Branchname
FROM Branch
JOIN mem_branch ON mem_branch.branchid=Branch.branchid
WHERE mem_branch.memberid = 2 --AND mem_branch.branchid = 1

In last line uncomment AND mem_branch.branchid = 1 if you really want to know name of only Branch with the id = 1 (what is a little bit strange, you may use direct select from table Branch without any connection to Members)

Upvotes: 1

Related Questions