所以,我打算做的是獲取與他們的類別和用戶的詳細信息條目/職位列表,它的每一個總髮表評論。 (項,類別,用戶和評論都是獨立的表)SQL幫助:COUNT聚合,參賽名單和評論數
下面這個查詢獲取記錄很好,但它似乎跳過沒有評論的條目。據我所知,JOIN是好的(註釋表上的LEFT JOIN),並且查詢是正確的。我錯過了什麼 ?
SELECT entries.entry_id, entries.title, entries.content,
entries.preview_image, entries.preview_thumbnail, entries.slug,
entries.view_count, entries.posted_on, entry_categories.title AS category_title,
entry_categories.slug AS category_slug, entry_categories.parent AS category_parent,
entry_categories.can_comment AS can_comment, entry_categories.can_rate AS can_rate,
users.user_id, users.group_id, users.username, users.first_name, users.last_name,
users.avatar_small, users.avatar_big, users.score AS user_score,
COUNT(entry_comments.comment_id) AS comment_count
FROM (entries)
JOIN entry_categories ON entries.category = entry_categories.category_id
JOIN users ON entries.user_id = users.user_id
LEFT JOIN entry_comments ON entries.entry_id = entry_comments.entry_id
WHERE `entries`.`publish` = 'Y'
AND `entry_comments`.`publish` = 'Y'
AND `entry_comments`.`deleted_at` IS NULL
AND `category` = 5
GROUP BY entries.entry_id, entries.title, entries.content,
entries.preview_image, entries.preview_thumbnail, entries.slug,
entries.view_count, entries.posted_on, category_title, category_slug,
category_parent, can_comment, can_rate, users.user_id, users.group_id,
users.username, users.first_name, users.last_name, users.avatar_big,
users.avatar_small, user_score
ORDER BY posted_on desc
編輯:我使用的MySQL 5.0
取代: 「AND`entry_comments`.`publish` = 'Y'」 與 「AND ISNULL(`entry_comments`.`publish`, 'Y')= 'Y'」 的結果在語法錯誤。或者我弄錯了? – andyk 2009-07-21 07:51:50
對不起,在MySQL中,它的IFNULL不ISNULL - 編輯答案。 – 2009-07-21 08:29:54
好的,IFNULL對我來說是新的。你如何考慮`deleted_at`?作爲計算應予以公佈,並有`在NULL – andyk 2009-07-21 08:54:19