Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to implement join properly
    primarykey
    data
    text
    <p>I am trying to figure out</p> <p>(book_id, branch_id, total_copies, number_available)</p> <p>Here is the schema: <img src="https://i.stack.imgur.com/K4qhp.jpg" alt="enter image description here"></p> <p>Here is as far I have gone:</p> <pre><code>SELECT TITLE, A.BOOK_ID, A.BRANCH_NAME, A.BRANCH_ID, NO_OF_COPIES, COUNT(BOOK_LOANS.BOOK_ID) AS NUM_OUT, NO_OF_COPIES - COUNT(BOOK_LOANS.BOOK_ID) AS NUM_AVAIL, AUTHOR_NAME FROM (SELECT TITLE, BOOK.BOOK_ID, BOOK_COPIES.BRANCH_ID, BRANCH_NAME, NO_OF_COPIES, BOOK_AUTHORS.AUTHOR_NAME FROM BOOK, BOOK_COPIES, LIBRARY_BRANCH, BOOK_AUTHORS WHERE BOOK_COPIES.BOOK_ID=BOOK.BOOK_ID AND BOOK_AUTHORS.BOOK_ID = BOOK.BOOK_ID AND ( BOOK.TITLE LIKE :title OR BOOK.BOOK_ID = :book_id OR BOOK_AUTHORS.AUTHOR_NAME LIKE :author_name ) AND BOOK_COPIES.BRANCH_ID=LIBRARY_BRANCH.BRANCH_ID) AS A LEFT OUTER JOIN BOOK_LOANS ON A.BOOK_ID=BOOK_LOANS.BOOK_ID AND A.BRANCH_ID=BOOK_LOANS.BRANCH_ID GROUP BY A.BOOK_ID, A.BRANCH_ID </code></pre> <p>But the problem I am encountering is, since BOOK_AUTHORS table has multiple authors for a single book. So I am able to understand that joining only on book_id of BOOK and BOOK_AUTHORS while I should be able to completely using the both book_id and author_name.</p> <p>So its returning the count 2 times for a single entry in book_loans.</p> <p>e.g. If a book with book_id lets say 1234 is checked out once, and it has total copies of 2, so it should return remaining copies as 1. But its returning 0.</p> <p>Please help me understand how to fix this.</p> <p>Note: I am using php prepare statement.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload