I’m currently working on optimizing a MySQL query, and I’m wondering if there’s a way to retrieve both the count of the results and the actual result set in a single query. I’m aware that I can achieve this using separate queries, one for the count and one for fetching the results, but I’m curious if there’s a more efficient way to accomplish this within a single query.
>Solution :
Yes, you can use a subquery in SELECT :
Select *, (SELECT COUNT(id) FROM city) as total FROM city;
And here is the result :