I have two tables in the database:
Article
--------------------------------------------
articleID INTEGER PRIMARY KEY AUTOINCREMENT
userID VARCHAR
Rating
--------------------------------------------
articleID INTEGER
userID VARCHAR
rating INTEGER
Sample data:
Article
articleID userID
---------------------------------
1 12345
2 23456
3 23456
4 99999
5 15678
Rating
articleID userID rating
--------------------------------------------
1 12345 7.5
2 12345 8.5
2 31359 7.5
1 24021 0.0
1 25012 7.5
I want to get all the articles and show if I have rated them or not. I tried and I can only get the list with rated article with this statement:
SELECT a.*, rating AS myRating FROM Article AS a
LEFT JOIN Rating AS b
ON a.articleID = b.articleID
WHERE (b.userID is NULL || b.userID = "12345")
How can I join the tables to get the result?
Expected output (with userID 12345)
articleID userID myRating
--------------------------------------------
1 12345 7.5
2 23456 8.5
3 23456 NULL
4 99999 NULL
5 15678 NULL
The WHERE-clause will filter out the columns where userID is not 12345 or NULL…
Since you want to show all lines of a, put the user-id restriction in the
ONclause. Now it will get all lines from A, with NULL for rating if the userid was not 12345.