Answer the question
In order to leave comments, you need to log in
How to list a user's unread posts?
There is a table users
There is a table articles
After the user on the site goes to the news, it will automatically be as read. That is, there is a read_articles table.
Actually, its structure is:
id | article_id | user_id
SELECT articles.id, articles.title, articles.text FROM articles INNER JOIN read_articles ON articles.id = read_articles.article_id WHERE read_articles.user_id = 2;
SELECT articles.id, articles.title, articles.text FROM articles INNER JOIN read_articles ON articles.id != read_articles.article_id WHERE read_articles.user_id = 2;
Answer the question
In order to leave comments, you need to log in
In the second option, you have tables connected incorrectly, so there are problems.
There is a NOT IN
construct
. Something like this:
SELECT articles.id, articles.title, articles.text FROM articles WHERE articles.id NOT IN ( SELECT articles.id FROM articles WHERE user_id = some_id )
SELECT articles.id, articles.title, articles.text FROM articles WHERE id NOT IN(SELECT article_id FROM read_articles WHERE user_id = ?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question