I have two tables: BOOKS and USERS_BOOKS:
BOOKS:
| ID | BOOKNAME |
|----|----------|
| 1 | Book1 |
| 2 | Book2 |
| 3 | Book3 |
| 4 | Book4 |
| 5 | Book5 |
USERS_BOOKS:
| ID | USERID | BOOKID | STATUS |
|----|--------|--------|--------|
| 1 | 001 | 1 | Read |
| 2 | 001 | 2 | Read |
| 3 | 001 | 3 | Added |
| 4 | 002 | 1 | Added |
| 5 | 002 | 5 | Added |
| 6 | 003 | 2 | Read |
| 7 | 004 | 4 | Read |
http://sqlfiddle.com/#!2/9cff9/1
From this sqlfiddle I can query a list of books and the number people who read them.
select BOOKS.ID, BOOKS.BOOKNAME,
SUM(CASE WHEN USERS_BOOKS.STATUS='Read' THEN 1 ELSE 0 END) AS NUM_READ
from BOOKS
LEFT JOIN USERS_BOOKS ON USERS_BOOKS.BOOKID = BOOKS.ID
GROUP BY BOOKS.ID
http://sqlfiddle.com/#!2/9cff9/1
What I need to add to this query is that I want to see if a specific user (USERID 001) read these books or not. So in a fourth column I want to display that I READ the first book (YES), READ the second book, NOTREAD the third, the fourth and the fifth books either (NO). (One person read the fourth book but that was not me).
DESIRED RESULT:
| ID | BOOKNAME| NUM_READ | DID_I_READ_IT|
|----|---------|----------|--------------|
| 1 | BOOK1 | 1 | YES |
| 2 | BOOK2 | 2 | YES |
| 3 | BOOK3 | 0 | NO |
| 4 | BOOK4 | 1 | NO |
| 5 | BOOK5 | 0 | NO |