获取具有组条件的查询中的整个行数

时间:2013-09-01 09:07:33

标签: mysql

我需要在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个不正确的结果。你能告诉我如何让它按照我的需要工作吗?感谢。

1 个答案:

答案 0 :(得分: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 '%%')