加入两个表和count -statistics [英] Join two tables and count -statistics
问题描述
需要编写显示统计信息的Mysql查询,
描述:
父表60 lac记录包含唯一记录
包含1亿卢比的子表包含多个记录
Need to write Mysql query showing statistics,
Description:
Parent table 60 lac records contains unique records
Child table with 1 crore Contains multiple records
Parent Table
ID Flag1 Flag2
1 1 1
2 NULL 1
3 1 1
Child Table
ID acc Category
1 101 Cate1
1 102 Cate5
2 103 Cate2
2 104 Cate3
3 105 Cate4
3 106 Cate4
两张桌子之间的一对多关系。
我需要通过查询编写组来查找使用MySQL数据库的一个查询中的分类和基于标志的计数
样本:
我尝试过以下查询但是我不想写每个类别的条件和Flag作为Child表包含30个类别和12个标志
One to many relationship between two tables.
I need to write group by queries to find categoriwise and flag based count in one query with MySQL Database
sample:
I have tried with following query but i dont want to write where condition for each category and Flag as Child table contains 30 categories and 12 Flags
select count(1) from Parent A,Child B where A.ID=B.ID AND B.Category='CATE1' and A.Flag1=1
预期结果
Result expected
Category Flag1_Count Flag2_Count
Cate1 1 0
Cate2 0 1
Cate3 1 1
请帮助。
Please help.
推荐答案
查询将如下所示
The query will be like as follows
SELECT B.Category, Count(A.Flag1) AS Flag1_Count, Count(A.Flag2) AS Flag2_Count FROM Parent A
INNER JOIN Child B WHERE A.ID=B.ID
GROUP BY B.Category;
这篇关于加入两个表和count -statistics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!