Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

SQL cartesian problem

  • 04-05-2012 03:14PM
    #1
    Registered Users, Registered Users 2 Posts: 7,838 ✭✭✭


    I've got a left outer join query returning a cartesian product and I can't see where the problem is. Can you have a look please?
    SELECT username, loan_date, copy_id, book_title 
    FROM members m 
    LEFT OUTER JOIN membership_card mc ON m.mem_id = mc.mem_id 
    LEFT OUTER JOIN loans l ON l.mem_id = mc.mem_id 
    LEFT OUTER JOIN book_copies bc ON l.isbn = bc.isbn 
    LEFT OUTER JOIN books b ON b.isbn = bc.isbn;
    

    Will provide ERD screenshot in a sec


Comments

  • Registered Users, Registered Users 2 Posts: 7,838 ✭✭✭Nulty


    It just clicked :rolleyes:
    SELECT username, loan_date, copy_id, book_title 
    FROM members m 
    LEFT OUTER JOIN membership_card mc ON m.mem_id = mc.mem_id 
    LEFT OUTER JOIN loans l ON l.mem_id = mc.mem_id 
    LEFT OUTER JOIN book_copies bc ON l.isbn = bc.isbn AND bc.copy_id = l.copy_id 
    LEFT OUTER JOIN books b ON b.isbn = bc.isbn;
    

    The loans table has two foreign keys in book copies fields, I needed to specify the join on both keys.


Advertisement
Advertisement