gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:06:44 25 4
gpt4 key购买 nike

我有这3张 table

论坛论坛

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

论坛话题

+----+-----+-----+-------+-------+------+-----------+------+
| 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 |
+----+-----+-----+-------+-------+------+-----------+------+

论坛帖子

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

我正在尝试创建返回结果的查询 -> 对于每个唯一的“fid”,来自“forums_posts”的一行(按“日期”排序)。

forums_forum.fid = forums_threads.fidforums_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
);

关于php - 每个论坛最新帖子的 MySQL 查询设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com