Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to obtain both the count and results of a query in a single MySQL query?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yes, you can use a subquery in SELECT :

Select *, (SELECT COUNT(id) FROM city) as total FROM city;

And here is the result :

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading