項目是基於:thinkcmf的,新的需求是對各欄目的文章數量進行統計
SQl很簡單,先根據分類ID進行分組,然后再通過CASE WHEN 再統計不同文章狀態數量
SELECT t.name,t.parent,t.term_id,count(1) as count ,COUNT( CASE WHEN p.post_status = 1 then 1 else null end ) as p1 # 1=已審核 ,COUNT( CASE WHEN p.post_status = 0 then 0 else null end ) as p0 # 0=未審核 ,COUNT( CASE WHEN p.post_status = -1 then -1 else null end ) as p_1 #-1=審核未通過 FROM term_relationships r join terms t on r.term_id= t.term_id join posts p on p.id = r.object_id group by t.term_id #以分類ID分組
