I'm trying to count the number of rows returned by a query, which includes Sum, Match, Against and Group By statements.
I've written an SQL statement that performs a query on a search index table and then groups the results.
I'm trying to count the results, so that I can set up pagination.
Whenever I've used the Count functionality before, it's been to count the rows in a table and not the results of a query.
I've also explored the SQL_CALC_FOUND_ROWS option with SELECT FOUND_ROWS() afterwards, but I'm not getting the results required.
Search Query...
SELECT *, SUM(MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE)) as score FROM `search_index` WHERE MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE) GROUP BY `Field1`, `Field2` ORDER BY score DESC;
Count Attempt 1...
SELECT COUNT(*), SUM(MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE)) as score FROM `search_index` WHERE MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE) GROUP BY `Field1`, `Field2`
Count Attempt 2... (Based on this post How can I count the numbers of rows that a mysql query returned?)
SELECT SQL_CALC_FOUND_ROWS *, SUM(MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE)) as score FROM `search_index` WHERE MATCH(`Field1`, `Field2`, `Field3`, `Field4`) AGAINST('test' IN BOOLEAN MODE) GROUP BY `Field1`, `Field2`;
SELECT FOUND_ROWS();
Attempt 1 - The result I was looking for was 89, but I was returned 89 rows with a count of 1 in each.
Attempt 2 - The result I was looking for was 89, but I was returned 1 row with a count of 1 in it.
Any advice would be greatly appreciated!