How many rows in a table
SELECT COUNT(*) AS Count FROM tblMyTable;
$Output = 0;
if (count($Results) > 0)
$Output = $Results[0]['Count'];
return($Output);
Count and Group Combined
List the number of customers in each country
SELECT COUNT(Id), Country FROM tblMyTable GROUP BY Country
Adding values returned as a the total
Use SUM()
Counting multiple things in a single query
SELECT
COUNT(*) AS TotalUsers,
COUNT(CASE WHEN EmailAddress != '' THEN 1 END) AS EmailAddressCount,
COUNT(CASE WHEN UserAcceptsMarketing = 1 THEN 1 END) AS UserAcceptsMarketingCount,
COUNT(CASE WHEN Options != 0 THEN 1 END) AS OptionsCount,
COUNT(CASE WHEN Joined > NOW() - INTERVAL 30 DAY THEN 1 END) AS JoinedInLast30DaysCount,
COUNT(CASE WHEN LastActive > NOW() - INTERVAL 30 DAY THEN 1 END) AS ActiveInLast30DaysCount
FROM my_table;
