简体   繁体   English

如何在MySQL中通过group by获取两个不同字段的计数?

[英]How to get count of two different fields with group by in MySQL?

I have a "4 table joined sql query". 我有一个“ 4表联接的sql查询”。 There are relations between them. 它们之间有关系。 Tables names are, users, documents, document_type, document_type_group. 表名称是,用户,文档,document_type,document_type_group。 documents table has all id named uniq identifiers from other tables. documents表具有其他表中所有名为id的uniq标识符。 I try to get counts of documents in document_types and total count of document_type_groups. 我尝试获取document_types中的文档数和document_type_groups的总数。 (Table names are Turkish, sorry for that) I can get count of document_type_groups with the query below: (表名是土耳其语,对此感到抱歉),我可以通过以下查询获取document_type_groups的数量:

SELECT eTG.evrak_tipi_grup_adi, eT.evrak_tipi, COUNT(eTG.evrak_tipi_grup_adi) AS toplamGrup
FROM evraklar evr
LEFT JOIN kullanicilar kull ON evr.evrak_kaydeden_ybs_kullanici_id=kull.id
LEFT JOIN evrak_tipleri eT ON evr.evrak_tipi=eT.id
LEFT JOIN evrak_tipi_gruplari eTG ON evr.evrak_tipi_grubu=eTG.id
GROUP BY eTG.evrak_tipi_grup_adi

and I can get count of document_type with: 我可以通过以下方式获取document_type的计数:

SELECT eTG.evrak_tipi_grup_adi, eT.evrak_tipi, COUNT(eT.evrak_tipi) AS toplamTiptekiler
FROM evraklar evr
LEFT JOIN kullanicilar kull ON evr.evrak_kaydeden_ybs_kullanici_id=kull.id
LEFT JOIN evrak_tipleri eT ON evr.evrak_tipi=eT.id
LEFT JOIN evrak_tipi_gruplari eTG ON evr.evrak_tipi_grubu=eTG.id
GROUP BY eT.evrak_tipi

I wish to combine these querys to show them on same table to user. 我希望将这些查询组合在一起,以显示给用户。 Any clue? 有什么线索吗?

Looking at your SQL, I am a bit unsure what you want to occur. 查看您的SQL,我有点不确定要发生什么。

Both pieces retrieve eTG.evrak_tipi_grup_adi and eT.evrak_tipi, but one groups by one , while the 2nd groups by the other. 这两部分都检索eTG.evrak_tipi_grup_adi和eT.evrak_tipi,但是一组按一个分组,而第二组按另一个分组。 If the values vary of the non grouped field vary for a grouped field then the value that is returned is from an unspecified row, so is likely to be meaningless. 如果非分组字段的值随分组字段的不同而变化,则返回的值来自未指定的行,因此可能没有意义。

If the values do not vary (ie, any record on evrak_tipleri only has a single record on evrak_tipi_gruplari - presume docuement type and document type group) then your current queries could be written as :- 如果值没有变化(即,evrak_tipleri上的任何记录在evrak_tipi_gruplari上只有一条记录-假定文档类型和文档类型组),那么您当前的查询可以写为:-

SELECT eTG.evrak_tipi_grup_adi, eT.evrak_tipi, COUNT(eTG.evrak_tipi_grup_adi) AS toplamGrup
FROM evraklar evr
LEFT JOIN kullanicilar kull ON evr.evrak_kaydeden_ybs_kullanici_id=kull.id
LEFT JOIN evrak_tipleri eT ON evr.evrak_tipi=eT.id
LEFT JOIN evrak_tipi_gruplari eTG ON evr.evrak_tipi_grubu=eTG.id
GROUP BY eTG.evrak_tipi_grup_adi, eT.evrak_tipi

SELECT eTG.evrak_tipi_grup_adi, eT.evrak_tipi, COUNT(eT.evrak_tipi) AS toplamTiptekiler
FROM evraklar evr
LEFT JOIN kullanicilar kull ON evr.evrak_kaydeden_ybs_kullanici_id=kull.id
LEFT JOIN evrak_tipleri eT ON evr.evrak_tipi=eT.id
LEFT JOIN evrak_tipi_gruplari eTG ON evr.evrak_tipi_grubu=eTG.id
GROUP BY eTG.evrak_tipi_grup_adi, eT.evrak_tipi

so could just be merged to:- 所以可以合并到:

SELECT eTG.evrak_tipi_grup_adi, eT.evrak_tipi, COUNT(eTG.evrak_tipi_grup_adi) AS toplamGrup, COUNT(eT.evrak_tipi) AS toplamTiptekiler
FROM evraklar evr
LEFT JOIN kullanicilar kull ON evr.evrak_kaydeden_ybs_kullanici_id=kull.id
LEFT JOIN evrak_tipleri eT ON evr.evrak_tipi=eT.id
LEFT JOIN evrak_tipi_gruplari eTG ON evr.evrak_tipi_grubu=eTG.id
GROUP BY eTG.evrak_tipi_grup_adi, eT.evrak_tipi

The count will just bring back the number of non null values in those fields though. 计数只会带回那些字段中非空值的数量。 I suspect you might want to use COUNT(DISTINCT....) instead. 我怀疑您可能想使用COUNT(DISTINCT ....)。

However this very much depends on exactly what you want to count and how the tables relate to each other (ie, one to many relationships, etc) 但是,这很大程度上取决于您要计数的内容以及表之间的关系(即一对多关系等)。

EDIT 编辑

Ignoring users for now, but I think this is what you want (using the table and column names from the sql fiddle):-- 现在暂时忽略用户,但我想这就是您想要的(使用sql小提琴中的表名和列名):

SELECT doc_types.id, 
        doc_types.doc_type_name, 
        Sub1.doc_type_group_name, 
        COUNT(documents.id) AS DocsForDocType,
        Sub1.DocsForDocTypeGroup
FROM doc_types
LEFT OUTER JOIN documents
ON doc_types.id = documents.doc_type_id
LEFT OUTER JOIN
(
    SELECT doc_type_groups.id, doc_type_groups.doc_type_group_name, COUNT(documents.id) AS DocsForDocTypeGroup
    FROM doc_type_groups
    INNER JOIN documents 
    ON doc_type_groups.id = documents.doc_type_group_id
    GROUP BY doc_type_groups.id, doc_type_group_name
) Sub1
ON doc_types.doc_type_group_id = Sub1.id
GROUP BY doc_types.id, doc_types.doc_type_name, Sub1.doc_type_group_name, Sub1.DocsForDocTypeGroup;

This is getting the document types and the number of documents for that type, while also getting the document group for the document type and the number of documents in that group. 这将获取文档类型和该类型的文档数,同时还将获取文档类型的文档组和该组中的文档数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM