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

时间:2021-07-23 20:12:01

I need to have a whole number of rows in a query with a GROUP condition. Here is query

我需要在具有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`

and results as

和结果为

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

I need to get 12. If I remove GROUP condition I get 15 results which is not correct. Can you please advise me how to get it work as I need. Thanks.

我需要得到12.如果我删除GROUP条件,我得到15个不正确的结果。你能告诉我如何让它按照我的需要工作吗?谢谢。

1 个解决方案

#1


2  

Remove the GROUP BY and select COUNT(DISTINCT exp_channel_titles.entry_id) instead:

删除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 '%%')

#1


2  

Remove the GROUP BY and select COUNT(DISTINCT exp_channel_titles.entry_id) instead:

删除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 '%%')