단지를 요청할 그 이유는,
SELECT forum_categories.title, COUNT(forum_topics.id) AS total_topics, COUNT(forum_messages.id) AS total_replies, MAX(forum_messages.date) AS last_message
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id = forum_categories.id
JOIN forum_messages ON forum_messages.topic_id = forum_topics.id
GROUP BY forum_categories.id ORDER BY forum_categories.date
그리고 나는 단지 forum_messages.original=0
COUNT(forum_messages.id) AS total_replies
을 계산해야한다 :
EDIT 사실
내 진짜 쿼리이 하나입니다 필터링 결과 집합 customField=1
, 즉
SELECT title, COUNT(*) AS numTitle
FROM yourtable
WHERE customField = 0
GROUP BY title
ORDER BY numTitle DESC
결과 : (다른 countings이 필요한 경우에도 유용합니다)
SELECT title
, SUM(CASE WHEN customField=0 THEN 1 ELSE 0 END) AS numTitle
FROM yourtable
GROUP BY title
ORDER BY numTitle DESC
결과 :
aaa 2
bbb 1
ccc 0
aaa 2
bbb 1
은 0 수를 가지고있는 사람들을 포함한 모든 제목을 표시하려면 더 컴팩트 (하지만 MYSQL에서만 작동) :
SELECT title
, SUM(customField=0) AS numTitle
FROM yourtable
GROUP BY title
ORDER BY numTitle DESC
+1 : 이길테니까 –
초보자의 행운을! – Ryan
시험해 주셔서 감사합니다! 하지만, 나는 일종의'SELECT title, SUM (here) FROM'을 필요로합니다 ... – kwichz