mysql - Mysql count duplicate value different column
问题描述
I want to count the same value among parent_id and id_article, but it can be 0 if there is no same value among parent_id and id_article
table:t_article
id_article parent_id
441 0
1093 18
18 0
3141 3130
3130 0
3140 3130
3142 3130
Expected output
id_article parent_id Total
441 0 0
1093 18 0
18 0 1
3141 3130 0
3130 0 3
3140 3130 0
3142 3130 0
How do I make it happen?
解决方案
You can get your count by doing a sub clause and then join with your main query
select a.*, coalesce(b.cnt,0)
from t_article a
left join (
select parent_id, sum(parent_id <> 0) cnt
from t_article
group by parent_id
) b on (a.id_article = b.parent_id)
推荐阅读
- html - 使用 html 和 css 修复图像网格
- sql - 查询获取非活动用户
- python-3.x - Google 授权:错误 400:redirect_url_mismatch
- amazon-s3 - 使用 s3cmd,我如何在 s3 目录中按“上次修改”日期检索最新文件夹
- c# - 比较两个 HTML 字符串
- node.js - 如何在 NodeJS 项目中自动“审核”或“合理化”我的导入?
- xpath - Java XPath 前缀必须解析为命名空间
- javascript - 为什么这个来自 MDN 的简单 AJAX 示例会失败?
- python - 如何关闭与 Azure 服务总线队列的连接?
- java - 解析节点内具有相同属性的 XML 文件