MySQL查询结果波动

时间:2015-05-05 03:29:26

标签: 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; 

EXPLAIN SELECT:

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

2 个答案:

答案 0 :(得分:1)

这是由_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;

答案 1 :(得分:0)

  

LEFT JOIN forum_categories as fcats on fcats.id = ft2.topic_cat

我认为除了减慢处理速度之外,包含LEFT JOIN对结果没有影响。删除它。

  

ORDER BY fp2.id DESC

ORDER BY对结果没有影响,因为GROUP BY不关心。删除它。

如果这些都没有帮助,那么解释一下:

  

它所拉的帖子在3个帖子之间波动。

请提供EXPLAIN SELECT ...