SELECT
posts.id,posts.user_id,
username, title,posts.body as post_body, posts.created_at, posts.is_open, posts.views as views, users.photoUrl,
COUNT(DISTINCT answers.id) as answer_count,
COUNT(DISTINCT comments.id) as comment_count,
COUNT (CASE answers.approved WHEN 1 THEN 1 ELSE null END) as correct_count,
GROUP_CONCAT(tagname) as tags
FROM posts
LEFT JOIN posttag ON posts.id = post_id
JOIN tags ON tag_id = tags.id
JOIN users ON user_id = users.id
LEFT JOIN answers ON answers.post_id = posts.id
LEFT JOIN comments ON posts.id = comments.post_id
WHERE posts.id = ?;
我正在嘗試使用相應的答案、評論和標簽來查詢每個帖子。
但是在我的查詢結果中,當有 4 個標簽時,我得到 8 個標簽
COUNT correct_count 從 1 變為 4
我怎樣才能得到正確的資料?
uj5u.com熱心網友回復:
使用DISTINCT了correct_count和tags也:
.....................................
COUNT(DISTINCT CASE WHEN answers.approved THEN answers.id END) as correct_count,
GROUP_CONCAT(DISTINCT tagname) as tags
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/404832.html
標籤:
上一篇:需要從MySql表中過濾掉條件值
下一篇:MySql表中的問題、收據和余額
