Mysql 获取不带where子句的非零ID计数
我不想得到非零的漏斗id计数。我得到了漏斗id计数,但也显示了漏斗id的计数为零,在这里我们不添加where子句,在这个查询中我也得到了page_计数Mysql 获取不带where子句的非零ID计数,mysql,sql,Mysql,Sql,我不想得到非零的漏斗id计数。我得到了漏斗id计数,但也显示了漏斗id的计数为零,在这里我们不添加where子句,在这个查询中我也得到了page_计数 SELECT `smart_projects`.project_id, `smart_projects`.business_id, `smart_projects`.title, `page_pages`.`funnel_id` as `funnel_id`, count(distinct(page_pages.page_id) )as page
SELECT `smart_projects`.project_id, `smart_projects`.business_id, `smart_projects`.title,
`page_pages`.`funnel_id` as `funnel_id`, count(distinct(page_pages.page_id) )as page_count, count(distinct(page_pages.funnel_id) )as funnel_count
FROM `smart_projects`
LEFT JOIN `page_pages` ON `smart_projects`.`project_id` = `page_pages`.`project_id`
WHERE smart_projects.status != 0
AND `smart_projects`.`business_id` = 'cd9412774edb11e9'
AND `smart_projects`.`created_date` BETWEEN 1558031400 AND 1558722600
GROUP BY `smart_projects`.`project_id`
ORDER BY `funnel_count` ASC
LIMIT 10
page_pages表格为:
smart_项目表为:
结果是:-
预期结果是:
如果要过滤掉零值,请使用
having
子句:
SELECT sp.project_id, sp.business_id, sp.title,
count(distinct pp.page_id ) as page_count,
count(distinct pp.funnel_id ) as funnel_count
FROM `smart_projects` sp LEFT JOIN
`page_pages` pp
ON sp.`project_id` = pp.`project_id`
WHERE sp.status <> 0 AND
sp.`business_id` = 'cd9412774edb11e9' AND
sp.`created_date` BETWEEN 1558031400 AND 1558722600
GROUP BY sp.`project_id`
HAVING funnel_count > 0
ORDER BY `funnel_count` ASC
LIMIT 10;
选择sp.project\u id、sp.business\u id、sp.title、,
计数(不同的pp.page_id)为page_计数,
计数(不同的pp.funnel\u id)作为漏斗计数
来自'smart_projects'sp LEFT JOIN
`第页
在sp.`project\u id`=pp.`project\u id上`
其中sp.status 0和
sp.`business_id`=“cd9412774edb11e9”和
sp.“创建日期”介于1558031400和1558722600之间
按sp.`project_id分组`
漏斗计数大于0
按“漏斗计数”ASC订购
限制10;
注:
- 表别名使查询更易于编写和读取
不是一个函数,而是一个关键字。下面的表达式不需要括号DISTINCT
在funnel\u id
中不合适,因为它是聚合函数的参数SELECT
SELECT `smart_projects`.project_id, `smart_projects`.business_id, `smart_projects`.title,
`page_pages`.`funnel_id` as `funnel_id`, count(distinct(page_pages.page_id) )as page_count, count(distinct (CASE WHEN page_pages.funnel_id != 0 then page_pages.funnel_id ELSE NULL END ) ) as funnel_count
FROM `smart_projects`
LEFT JOIN `page_pages` ON `smart_projects`.`project_id` = `page_pages`.`project_id`
WHERE smart_projects.status != 0
AND `smart_projects`.`business_id` = 'cd9412774edb11e9'
GROUP BY `smart_projects`.`project_id`
ORDER BY `title` DESC