php - MySQL查询结果波动

标签 php mysql

我的问题在这篇文章中更清楚:Select all categories with latest post, user, and topic information

——————————————————————————————————————————

我有一个查询,可以提取我的论坛的类别列表以及该类别中的最新帖子。结果按预期返回,除了如果多次运行查询,LEFT JOIN fp1 中提取的 sub_post 信息会发生变化。

我第一次注意到这个问题是在查看我的网页并刷新几次时。它正在拉动的帖子在 3 个帖子之间波动。我不确定这怎么可能,除非查询有问题。

请查看我下面的查询,如果我做错了什么可以解释这种奇怪的行为,请告诉我。

干杯。

SELECT fc1.id AS cat_id, fc1.cat_name AS cat_name,
    fc1.cat_description AS cat_description, fc1.cat_views as cat_views, fp1.*
FROM forum_categories as fc1
LEFT JOIN (SELECT fp2.id AS sub_post_id,
                fp2.post_date as sub_post_date,
                fp2.post_topic as sub_post_topic,
                u2.id as sub_user_id, u2.username as sub_username,
                ft2.topic_subject as sub_topic_subject, ft2.topic_cat as sub_topic_cat
            FROM forum_posts as fp2
            LEFT JOIN users as u2 on fp2.post_by = u2.id
            LEFT JOIN forum_topics as ft2 on ft2.id = fp2.post_topic
            LEFT JOIN forum_categories as fcats on fcats.id = ft2.topic_cat
            ORDER BY fp2.id DESC)
as fp1 on fp1.sub_topic_cat = fc1.id
GROUP BY fc1.id; 

解释选择:

+----+-------------+------------+--------+-------------------------+-------------+---------+--------------------+------+-------------+
| id | select_type | table      | type   | possible_keys           | key         | key_len | ref                | rows | Extra       |
+----+-------------+------------+--------+-------------------------+-------------+---------+--------------------+------+-------------+
|  1 | PRIMARY     | fc1        | index  | PRIMARY,cat_name_unique | PRIMARY     | 8       | NULL               |    3 | Using where |
|  1 | PRIMARY     | <derived2> | ref    | <auto_key0>             | <auto_key0> | 9       | tpw.fc1.id         |    9 | NULL        |
|  2 | DERIVED     | fp2        | index  | NULL                    | PRIMARY     | 8       | NULL               |   92 | NULL        |
|  2 | DERIVED     | u2         | eq_ref | PRIMARY                 | PRIMARY     | 8       | tpw.fp2.post_by    |    1 | NULL        |
|  2 | DERIVED     | ft2        | eq_ref | PRIMARY                 | PRIMARY     | 8       | tpw.fp2.post_topic |    1 | NULL        |
|  2 | DERIVED     | fcats      | eq_ref | PRIMARY                 | PRIMARY     | 8       | tpw.ft2.topic_cat  |    1 | Using index |
+----+-------------+------------+--------+-------------------------+-------------+---------+--------------------+------+-------------+

我有 3 个表:forums_categories、forums_topics 和 forums_posts。我正在尝试列出类别以及该类别中的最新帖子。 forums_post 通过 post_topic 链接到 forums_topics,而 forums_topics 通过 topic_cat 链接到 forums_categories。

最佳答案

这是由 _pala 在另一个问题上解决的:https://stackoverflow.com/a/30048334/4864675

我正在处理导致奇怪行为的查询错误。谢谢_pala!

这是用户 _pala 为我提供的 SQL:

select fc.cat_name, fc.cat_description, fc.cat_views, u.username, fp.post_date, ft.topic_subject
  from forum_categories fc
inner join forum_topics ft
  on fc.id = ft.topic_cat
inner join forum_posts fp
  on fp.post_topic = ft.id
inner join users u
  on fp.post_by = u.id
inner join (
  select topic_cat, max(fp.id) most_recent_post
    from forum_topics ft
      inner join forum_posts fp
        on fp.post_topic = ft.id
  group by topic_cat
) q
  on q.topic_cat = ft.topic_cat
    and fp.id = q.most_recent_post;

关于php - MySQL查询结果波动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043783/

相关文章:

php - 您使用什么 PHP 应用程序设计/设计模式?

PHP 自动套用段落格式标签

php - 在 html 中使用 javascript 进行开/关开关

mysql - 如何在Mysql上连接多个表并结果4列记录

php - 从本地主机上的 PHP 文件访问 MySql 数据库

c# - Sonarqube for TFS 构建时响应错误 500

php - 跨平台 AES 加密是如何工作的?

javascript - 如何以 javascript 方式创建 php getter 和 setter

php - 基本PHP MYSQL表单未提交

php - 在 PHP 中如何最好地与 0 进行比较?