Okay, then you need to record for each user if each article has been read.
Each user X each article = M x N relationship = hook table.
A hook table connects (hooks together) two other tables. It is used for multi/multi relationships:
Code:
CREATE TABLE articles (
id INTEGER PRIMARY KEY
-- blah, blah, blah
);
CREATE TABLE users (
id INTEGER PRIMARY KEY
-- blah, blah, blah
);
CREATE TABLE users_articles_read (
user_id INTEGER NOT NULL,
article_id INTEGER NOT NULL,
PRIMARY KEY (user_id)
);
Then when the user reads an article, you insert the (user_id, article_id) pair into the hook table.
When a user logs in, you do a little set arithmetic to decide which articles go in the "new news" and "old news (archive)" categories. For new-news, you look for articles that don't have an "already-read-it" hook for this user:
Code:
-- News
SELECT
article.id
FROM
articles AS article
LEFT JOIN users_articles_read AS read
ON (article.id = read.article_id)
WHERE
read.user_id = ?
AND read.article_id IS NULL
For archive articles, you look for articles that DO have a matching "read-already" hook:
Code:
-- Archive
SELECT
article.id
FROM
articles AS article
JOIN users_articles_read AS read
ON (article.id = read.article_id)
WHERE
read.user_id = ?
(Note the "LEFT" join is gone.)
And in the interest of completeness, you count them thus:
Code:
-- Counts
SELECT
IF (read.article_id IS NULL, "unread", "read") AS status,
COUNT(*) as count
FROM
articles AS article
LEFT JOIN users_articles_read AS read
ON (article.id = read.article_id)
WHERE
read.user_id = ?
GROUP BY status
Bookmarks