나는 기본적으로 내 사용자에게, 내 데이터베이스에 저장 한 다른 포럼을 보여줍니다 꽤 큰 쿼리 가지고 : 다음 모든을 보여주기 위해 while 루프를 할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 (($row = $stmt->fetch())) {
echo $row['name'];
}
문제는 while 루프 동안, 나는 좋은 결과를 때때로을 얻을입니다!
그것은 다음과 같아야합니다
forum1
forum2
forum3
forum4
forum5
forum6
등
을하지만 때로는이 같은 결과를 얻을 :
forum1
forum1
forum1
forum1
forum2
forum3
forum4
forum5
forum6
행의 일부
있습니다 (이것은 어떤 행 상당히 랜덤입니다) '중복'되고있다.누군가 내가이 문제에 대해 도움을 줄 수 있습니까? GROUP BY t.forum_id
를 추가하여
은 아마도, 쿼리가'같은 오류가 발생 t' forum_topics로부터 t.forum_id SELECT? – Strawberry
@Strawberry 당신은 정교하게 신경 쓰겠습니까? – oliverbj
단순한 추측이지만 ... 내가 한 포럼에 대해 많은 게시물을 가지고있을 수 있으므로 메인 쿼리에 게시물을 가져 오는 세 번째 조인 (하위 쿼리)이 복제본을 만듭니다. 하위 쿼리를 혼자서 실행할 수 있습니까 (이름을'''lp'''라고 부름) 그리고 반복되는'''forum_id''' 필드가 있는지 점검 할 수 있습니까? – mTorres