Skip to content
Advertisement

How to Join 3 Tabels in Sqlserver

I tried to join 3 tables all together but when I execute the query only the first row on the table is displayed. The table consists of many rows. How can I display multiple rows?

This is the code that I tried

SELECT
l.id,m.name,b.bookname,l.issuedate,l.returndate
FROM lend l
INNER JOIN member m
    on l.memberid = m.id 
INNER JOIN books b ON b.id = l.bookid

Member table

enter image description here

Books table

enter image description here

lend table

enter image description here

data i need to join

ID membername Bookname issue_date return_date

Advertisement

Answer

This is my first post.

It is possible that one of the other tables that you have joined to does not contain the other 2 records. In order to see all records from [lend], you would do this:

SELECT l.id, m.name, b.bookname, l.issuedate, l.returndate 
FROM lend l 
LEFT OUTER JOIN member m ON l.memberid = m.id 
LEFT OUTER JOIN books b ON b.id = l.bookid

I am not sure which table contains the records that you are interested in, but select from only that table first. Check the record count. Then LEFT OUTER JOIN to one more table. Execute. Check record count. Keep going. That way you know which table join has affected the record count.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement