2014-09-21 41 views
0

我有相當大的查詢,這基本上顯示了不同的論壇我已經保存在我的數據庫,我的用戶:PHP - 加入語句複製記錄

$stmt = $dbh->prepare(" 
    SELECT 
     c.forum_id as category_id, 
     c.forum_name as category_name, 
     t.forum_id as id, 
     t.forum_name as name, 
     t.forum_desc as description, 
     (SELECT COUNT(*) FROM forum_topics WHERE forum_id=t.forum_id AND topic_deleted=0) as topics_count, 
     (SELECT COUNT(*) FROM forum_posts WHERE forum_id=t.forum_id AND post_deleted=0) as posts_count, 
     (SELECT COUNT(*) FROM forum_posts WHERE topic_id=lp.topic_id AND post_deleted=0) as last_post_count, 
     lp.topic_id as last_post_topic_id, 
     lp.topic_title as last_post_topic_title, 
     lp.post_time as last_post_time, 
     lp.username as last_post_username 
    FROM forum_cats as t 
    JOIN forum_cats as c on c.forum_id = t.forum_type_id 
    left join (
     SELECT 
      ft.topic_id, 
      ft.title as topic_title, 
      tmp.post_time, 
      u.username, 
      fp.forum_id 
     FROM 
      forum_posts fp 
      join forum_topics ft on ft.topic_id = fp.topic_id 
      join users u on u.id = fp.userid 
      join (
       select forum_id, max(`post_time`) `post_time` 
       from forum_posts fp 
       where fp.post_deleted = 0 
       group by forum_id 
       ) as tmp on (fp.forum_id = tmp.forum_id and fp.post_time = tmp.post_time) 
     where post_deleted = 0 and ft.topic_deleted = 0 
    ) as lp on lp.forum_id = t.forum_id 
    where t.forum_active = 1 and c.forum_active = 1 
    order by category_id, t.forum_id 
"); 

    $stmt->execute(); 

我然後做一個while循環來顯示所有的記錄:

while (($row = $stmt->fetch())) { 
echo $row['name']; 
} 

的問題是,while循環過程中,我獲得優秀成果有時

它應該是這樣的:

forum1 
forum2 
forum3 
forum4 
forum5 
forum6 

但有時我得到的結果是這樣的:

forum1 
forum1 
forum1 
forum1 
forum2 
forum3 
forum4 
forum5 
forum6 

某些行(這是相當隨機哪一行)是被「重複」。

有人能幫我解釋一下嗎?

+0

據推測,查詢'選擇t.forum_id FROM t'生成相同的錯誤forum_topics? – Strawberry 2014-09-21 08:16:39

+0

@Strawberry你願意詳細說明嗎? – oliverbj 2014-09-21 08:21:13

+0

只是一個瘋狂的猜測,但...我會說你的第三次加入(子查詢)是帶來的主要查詢職位使重複,因爲您可能有一個論壇的許多職位。你可以單獨運行子查詢(你把它命名爲'lp''')並檢查是否有重複的'''forum_id'''字段? – mTorres 2014-09-21 08:23:12

回答

0

,嘗試按加入GROUP BY t.forum_id

SELECT 
    c.forum_id as category_id, 
    c.forum_name as category_name, 
    t.forum_id as id, 
    t.forum_name as name, 
    t.forum_desc as description, 
    (SELECT COUNT(*) FROM forum_topics WHERE forum_id=t.forum_id AND topic_deleted=0) as topics_count, 
    (SELECT COUNT(*) FROM forum_posts WHERE forum_id=t.forum_id AND post_deleted=0) as posts_count, 
    (SELECT COUNT(*) FROM forum_posts WHERE topic_id=lp.topic_id AND post_deleted=0) as last_post_count, 
    lp.topic_id as last_post_topic_id, 
    lp.topic_title as last_post_topic_title, 
    lp.post_time as last_post_time, 
    lp.username as last_post_username 
FROM forum_cats as t 
JOIN forum_cats as c on c.forum_id = t.forum_type_id 
left join (
    SELECT 
     ft.topic_id, 
     ft.title as topic_title, 
     tmp.post_time, 
     u.username, 
     fp.forum_id 
    FROM 
     forum_posts fp 
     join forum_topics ft on ft.topic_id = fp.topic_id 
     join users u on u.id = fp.userid 
     join (
      select forum_id, max(`post_time`) `post_time` 
      from forum_posts fp 
      where fp.post_deleted = 0 
      group by forum_id 
      ) as tmp on (fp.forum_id = tmp.forum_id and fp.post_time = tmp.post_time) 
    where post_deleted = 0 and ft.topic_deleted = 0 
) as lp on lp.forum_id = t.forum_id 
where t.forum_active = 1 and c.forum_active = 1 
GROUP BY t.forum_id 
order by category_id, t.forum_id 
+0

我剛剛使用此查詢得到錯誤 – oliverbj 2014-09-21 08:27:55

+0

或者將第一個'SELECT'更改爲'SELECT DISTINCT'。畢竟,如果沒有任何東西需要聚合,你爲什麼要分組?無論哪種方式,這將消除症狀,但在許多情況下意外的重複行表明需要解決的錯誤。現在你只是壓制症狀。 – GolezTrol 2014-09-21 08:28:30

+0

如果你添加了'GROUP BY',它應該在'ORDER BY'之前。 – GolezTrol 2014-09-21 08:28:57