I am fetching records from one table with count of one field with other field as name. I want only 10 records. out of which 9 records give me field and its count. but i want to show 10 record as "Others" with all remaining fields with count. This is something like wrapping records. Something like below is table contents.
emp_id | designation
1 | software Engg.
2 | software Engg.
3 | Project Manager
not less than 10 designation.
And I want to show first 10 records as
Software Engineers 20
Project Manager 5
....
....
....
Others 50
Is there any way to make SQL Query for mysql db so that it will be fast and save time in application level where I am adding up record counts for "Others" ?
Please suggest how I can make it possible in effective way.
SELECT ... FROM blah WHERE ... LIMIT 9;
andSELECT COUNT() FROM blah WHERE ...;
and just subtract 9 from the count? adding a lot of logic into the query may negatively impact speed compared to two separate queries.COUNT()
in SQL is pretty fast. I don't have any data but I think aCOUNT()
would be alot faster than all the logic in Marian's answer