简体   繁体   中英

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

I have a "4 table joined sql query". There are relations between them. Tables names are, users, documents, document_type, document_type_group. documents table has all id named uniq identifiers from other tables. I try to get counts of documents in document_types and total count of document_type_groups. (Table names are Turkish, sorry for that) I can get count of document_type_groups with the query below:

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:

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.

Both pieces retrieve eTG.evrak_tipi_grup_adi and eT.evrak_tipi, but one groups by one , while the 2nd groups by the other. 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 :-

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.

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):--

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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