php – 每个论坛最新帖子的MySQL查询设计

前端之家收集整理的这篇文章主要介绍了php – 每个论坛最新帖子的MySQL查询设计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有这3张桌子

forums_forum

+-----+--------+-------------+-------+-----+
| fid | name   | description | index | cid |
+-----+--------+-------------+-------+-----+
|  36 | gdghdf | hjghj       |    54 |   5 |
|  45 | yutuy  | iuyi        |    99 |   6 |
+-----+--------+-------------+-------+-----+

forums_threads

+----+-----+-----+-------+-------+------+-----------+------+
| id | tid | fid | moved | mfrom | view | important | lock |
+----+-----+-----+-------+-------+------+-----------+------+
|  1 |   4 |  36 |     0 | NULL  |    0 |         0 |    0 |
|  2 |  12 |  36 |     0 | NULL  |    7 |         0 |    0 |
|  3 |   9 |  15 |     0 | NULL  |    0 |         0 |    0 |
+----+-----+-----+-------+-------+------+-----------+------+

forums_posts

+----+-------+--------+--------+---------------------+--------+--------+-----+
| id | title | detail | author | date                | edited | editby | tid |
+----+-------+--------+--------+---------------------+--------+--------+-----+
|  1 | asfsd | sdfsd  |      1 | 2010-07-01 21:31:29 |      0 | NULL   |   4 |
+----+-------+--------+--------+---------------------+--------+--------+-----+

我正在尝试创建返回结果的查询 – >对于每个独特的’fid’,来自’forums_posts’的一行(ORDER BY’日期’).

forums_forum.fid = forums_threads.fid
forums_threads.tid = forums_posts.tid

谢谢

最佳答案
这是Stack Overflow上经常出现的令人尊敬的最大n组问题.这是给出表格的解决方案:

SELECT p.* FROM forums_posts p JOIN forums_threads t ON p.tid = t.tid
WHERE NOT EXISTS (
    SELECT * FROM forums_posts p2 JOIN forums_threads t2 ON p2.tid = t2.tid
    WHERE t.fid = t2.fid AND p.date < p2.date
);
原文链接:https://www.f2er.com/mysql/432955.html

猜你在找的MySQL相关文章