加入两个计数查询没有给出正确的结果

问题描述

我试图加入两个查询来比较计数

SELECT count(customer_id),customer_id
FROM `blog_post`
group by customer_id

第二个查询

SELECT count(customer_id)
FROM `blog_comment`
WHERE `is_admin` IS NOT NULL
group by customer_id

我创建的联合查询

SELECT count(post.customer_id) as post_count,post.customer_id,count(comment.customer_id) 
FROM `blog_post` as post 
    left join blog_comment as comment on post.customer_id = comment.customer_id 
WHERE `is_admin` IS NOT NULL 
GROUP BY post.customer_id 

我没有得到与单独运行它们相同的结果,我做错了什么

解决方法

根据您的要求,您需要 2 个查询中的 refreshTable() { if (this.gridColumnApi.columnController && this.gridColumnApi.columnController.bodyWidthDirty) { this.gridApi.sizeColumnsToFit(); if (this.refreshAttempts) { this.refreshAttempts--; setTimeout(() => { this.refreshTable(); },100); } } else { this.refreshAttempts = 0; } } ,MySql 不支持该查询,只能使用 FULL OUTER JOIN 连接和 LEFT/RIGHT 进行模拟。
>

另一种方法是对 2 个查询使用 UNION ALL 并聚合结果:

UNION ALL