Search code examples
mysqlsqlxampp

how to select the count of two different things at once in mysql?


SELECT COUNT(*), 
tanggal as totRits AND COUNT(*), 
total AS tNote  
FROM tblsolar 
WHERE tanggal LIKE '%" & sqlDate & "%' 
AND supir LIKE '%" & cboSupir & "%' 
GROUP BY tanggal

How do I SELECT the COUNT two times?


Solution

  • If you want to get the total COUNT and a COUNT of a specific column using different conditions, you can use a subquery like this:

    SELECT 
      (SELECT 
       COUNT(tanggal) AS totRits
       FROM tblsolar 
       WHERE tanggal LIKE '%sqlDate%' 
       AND supir LIKE '%cboSupir%' 
       GROUP By tanggal) AS totRits,
    COUNT(*) AS tNote
    FROM tblsolar
    

    Input:

    tanggal supir
    sqlDate cboSupir
    test test
    test cboSupir
    test cboSupir
    sqlDate cboSupir

    Output:

    totRits tNote
    2 5

    Adjust your WHERE clause conditions as needed for both the main query and subquery.

    db<>fiddle here.