我无法弄清楚如何为它进行mysql查询。 我有主题(id,名称)和帖子(用户,主题,帖子,时间)和用户上次主题视图(主题,用户,时间)的mysql表
我需要获取用户上次查看主题后编写的新帖子计数。
首先应该是获取最后的视图并将其添加到数组中,然后获取主题数组?我应该做什么样的mysql查询,包括来自这两个数组的数据?
结果应为(id => count):
array(1 => 2, 2 => 0, 3 => 15)
然后我可以做:
first topic (2)
second topic (0)
third topic (15)
答案 0 :(得分:0)
主题
id | name |
-------------------
1 | bla |
2 | lala |
帖子
topic_id | user | time |
--------------------------------------------
1 | X | 2013-08-09 12:00:00 |
1 | Y | 2013-08-09 13:00:00 |
2 | Y | 2013-08-09 12:00:00 |
--------------------------------------------
USER_VIEWS
topic_id | user_id | posts | read |
---------------------------------------------
NULL | NULL | NULL | NULL |
---------------------------------------------
首次用户打开论坛时,您需要添加新帖子
INSERT INTO `user_views` (`topic_id`, `user_id`, `posts`, `read`)
VALUES (
(SELECT `id` FROM `topics`),
$_SESSION['user_id'],
(SELECT COUNT(`topic_id`) FROM `posts` GROUP BY `topic_id`),
0
);
所以,它变成了:
USER_VIEWS
topic_id | user_id | posts | read |
---------------------------------------------
1 | 5 | 2 | 0 |
2 | 5 | 1 | 0 |
---------------------------------------------
用户打开topic_id = 1,因此帖子会自动读取(您可能无法跟踪每个帖子的阅读方式)。所以关于这个话题的确切开头:
UPDATE `user_views` SET `read` = (SELECT COUNT(`topic_id`) FROM `posts` WHERE `topic_id` = $_GET['topic_id'] AND user_id = $_SESSION['user_id'];
现在变成了:
USER_VIEWS
topic_id | user_id | posts | read |
---------------------------------------------
1 | 5 | 2 | 2 |
2 | 5 | 1 | 0 |
---------------------------------------------
现在在topic_id = 1:
中发布了一个新帖子帖子
topic_id | user | time |
--------------------------------------------
1 | X | 2013-08-09 12:00:00 |
1 | Y | 2013-08-09 13:00:00 |
2 | Y | 2013-08-09 12:00:00 |
1 | Z | 2013-08-09 14:00:00 |
--------------------------------------------
用户打开论坛,检查是否有新主题插入帖子和视图,如果没有,只执行此操作:
UPDATE `user_views` SET `posts` = (SELECT COUNT(`topic_id`) FROM `posts` WHERE `topic_id` = (SELECT `id` FROM `topics` WHERE `id` = `posts`.`topic_id`) AND user_id = $_SESSION['user_id'];
所以它变成了:
USER_VIEWS
topic_id | user_id | posts | read |
---------------------------------------------
1 | 5 | 3 | 2 |
2 | 5 | 1 | 0 |
---------------------------------------------
要检索未读帖子,您可以执行以下操作:
SELECT `topic_id`, `user_id`, `posts`-`read` as 'unread' FROM `user_views` WHERE user_id = $_SESSION['user_id'];
这将返回:
USER_VIEWS
topic_id | user_id | unread |
----------------------------------
1 | 5 | 1 | // 3-2
----------------------------------
2 | 5 | 1 | // 1-0
----------------------------------
因此,从上次访问开始,用户现在每个主题只有一个新帖子。
我希望你有逻辑。也许在会话或cookie中存储已经查看过的帖子更简单,但对我来说这似乎不可靠,所以我觉得新表是可以的。
如何在用户访问论坛时首先插入新主题,具体取决于您的应用逻辑。
当然,查询可能有一些错误,我试图让它们尽可能可行。它们只是示例,您必须使它们适合您的应用程序,即不要在查询中直接注入$ _GET或$ _SESSION。
PS:我建议在id
表中使用自动增量键列POSTS
,这样它的效果会更好,同时查询将针对id
,而不是topic_id即{{1} }
答案 1 :(得分:0)
SELECT posts.topic,
(SELECT COUNT(*)
FROM posts
WHERE posts.`time` > views.`time`
AND posts.`topic` = topics.`id`) AS counts
FROM posts
LEFT JOIN topics
ON topics.`id` = posts.`topic`
LEFT JOIN views
ON topics.`id` = views.`topic`
答案 2 :(得分:0)
尝试
SELECT t.id, t.name, COUNT(*) new_posts
FROM posts p JOIN topics t
ON p.topic_id = t.id LEFT JOIN views v
ON t.id = v.topic_id
AND v.user_id = 1
WHERE p.time > COALESCE(v.time, 0)
GROUP BY t.id, t.name
HAVING COUNT(*) > 0
示例输出:
| ID | NAME | NEW_POSTS | --------------------------- | 1 | topic1 | 2 | | 2 | topic2 | 1 |
这是 SQLFiddle 演示