I have ported a search from Coldfusion into a MySQL stored procedure.
The actual search is run twice on the site. Once to get the number of records, the 2nd time to get the actual results to display.
So both MySQL syntax differ in the last two lines:
Get no of records:
SELECT COUNT(*) ...
GROUP BY a, b, c
HAVING ...
Actual results:
SELECT "rows"...
GROUP BY a, b, c
HAVING ...
ORDER BY var1, var2
LIMIT var_start, var_end
My question:
Since I need to run this twice, is there a way to at least use the same stored procedure = can I add default values to order/grouping “ORDER BY ‘nothing’ LIMIT ‘all’, and a parameter to the SELECT?
Sort of like an if statement
WHERE 1
AND IF( var_x = '', '.', var_x = some_value )
Thanks for input!
The first query is wrong – you shouldn’t ask the MySQL engine to actually return the rows, and then count them, you should ask MySQL to count them directly:
Trying to shoe-horn both record count and record retrieval in the same SQL statement or stored procedure is an unnecessary optimisation.