HammockKing
HammockKing

Reputation: 77

Achieving a double join in sql

I have three tables: Books, Members and loanRecords.

The Books table has bookname,bookid, etc..

The Members table has memberid,membername etc..

The Loan records hold who has loaned what as in: bookid,memberid,loandate etc...

What I am trying to do is write a join which can list all the member with a specific book title out on loan and i may be getting a little confused. I know i have to

SELECT * from member"+
"JOIN member_loans_book on member_loans_book.Member_id=member.id "
+ " AND member_loans_book.Book_id=...

but when i get this far i get flabbergasted. Any help would be appreciated. Thanks

Upvotes: 0

Views: 142

Answers (2)

Friso van Dijk
Friso van Dijk

Reputation: 669

I'm no expert on sql in java, but I can create the query you want:

SELECT *
FROM Member m
JOIN Loans l ON l.memberid=m.memberid
JOIN Books b ON l.bookid=b.bookid
WHERE b.title = 'BookTitle'

Upvotes: 1

Tomasz Bekas
Tomasz Bekas

Reputation: 686

You're doing pretty good. This select might be something what you're looking for.

SELECT * FROM member m
    JOIN member_loans_book mlb ON m.id = mlb.member_id
    JOIN book b ON b.id = mlb.book_id
    WHERE b.title = 'MyBook';

Upvotes: 1

Related Questions