I have a library of books (books
) and another table where every time someone loans a book or brings a book back, it gets recorded (book_loans
).
Basically I want to see for all books that were signed out at a certain time, how many:
- were of the genre "fantasy"
- had more than 500 pages
- were hardcover books
I want to do this all at once and I'm using 4 queries right now, is there a better way?
PS: book_loans
.bookID is a foreign key for books
.id:
SELECT * FROM
(
SELECT COUNT(*) FROM `books` AS bks
INNER JOIN `book_loans` AS bs ON bks.id = bs.bookID
WHERE bs.time = '2017-07-01 02:16:00' AND bs.signedout = 1 AND bks.genre = 'fantasy'
UNION
SELECT COUNT(*) FROM `books` AS bks
INNER JOIN `book_loans` AS bs ON bks.id = bs.bookID
WHERE bs.time = '2017-07-01 02:16:00' AND bs.signedout = 1 AND bks.pages > 500
UNION
SELECT COUNT(*) FROM `books` AS bks
INNER JOIN `book_loans` AS bs ON bks.id = bs.bookID
WHERE bs.time = '2017-07-01 02:16:00' AND bs.signedout = 1 AND bks.hardcover = 1
) x;
It just seems very inefficient to be doing 4 queries total for this.