How to get the total number of rows of a GROUP BY query? How to get the total number of rows of a GROUP BY query? sqlite sqlite

How to get the total number of rows of a GROUP BY query?


Here is the solution for you

$sql="SELECT count(*) FROM [tablename] WHERE key == ? ";$sth = $this->db->prepare($sql);$sth->execute(array($key));$rows = $sth->fetch(PDO::FETCH_NUM);echo $rows[0];


It's a little memory-inefficient but if you're using the data anyway, I use this frequently:

$rows = $q->fetchAll();$num_rows = count($rows);


The method I ended up using is very simple:

$query = 'SELECT a, b, c FROM tbl WHERE oele = 2 GROUP BY boele';$nrows = $db->query("SELECT COUNT(1) FROM ($query) x")->fetchColumn();

Might not be the most efficient, but it seems to be foolproof, because it actually counts the original query's results.