2013-09-01 37 views
2

我需要在查詢中有一個具有GROUP條件的整數行。下面是查詢獲得組條件查詢中行的全部計數

SELECT COUNT(*) AS `numrows` 
FROM (`exp_channel_titles`) 
LEFT JOIN `exp_category_posts` cp ON `cp`.`entry_id`=`exp_channel_titles`.`entry_id` 
LEFT JOIN `exp_categories` c ON `cp`.`cat_id`=`c`.`cat_id` 
LEFT JOIN `exp_internships_placements` ip ON `ip`.`entry_id`=`exp_channel_titles`.`entry_id` 
LEFT JOIN `exp_members` m ON `m`.`member_id`=`exp_channel_titles`.`author_id` 
WHERE `exp_channel_titles`.`site_id` = '8' 
AND `exp_channel_titles`.`channel_id` = '7' 
AND (title like '%%') 
GROUP BY `exp_channel_titles`.`entry_id` 

和結果

numrows 
3 
2 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 

我需要得到12.如果我刪除GROUP條件,我得到15分的結果是不正確的。您能否請我告訴我如何根據需要使用它。謝謝。

回答

2

取出GROUP BY和選擇,而不是COUNT(DISTINCT exp_channel_titles.entry_id)

SELECT COUNT(DISTINCT `exp_channel_titles`.`entry_id`) AS `numrows` 
FROM (`exp_channel_titles`) 
LEFT JOIN `exp_category_posts` cp ON `cp`.`entry_id`=`exp_channel_titles`.`entry_id` 
LEFT JOIN `exp_categories` c ON `cp`.`cat_id`=`c`.`cat_id` 
LEFT JOIN `exp_internships_placements` ip ON `ip`.`entry_id`=`exp_channel_titles`.`entry_id` 
LEFT JOIN `exp_members` m ON `m`.`member_id`=`exp_channel_titles`.`author_id` 
WHERE `exp_channel_titles`.`site_id` = '8' 
AND `exp_channel_titles`.`channel_id` = '7' 
AND (title like '%%')