How to add count of different query using a query in mysql5?
Hi I need a query like this
select count(id) from employee where.. => gives count1=10 select count(id) from emplyer where.. => gives count2=5 select count(id) from users where.. => gives count3=20
I can add these result like this
count = count1+count2+count3;
But I want to know whether t开发者_如何学JAVAhere is any option get count using a single query instead of add these result separatly. Also I want to know which one is the best way in case of application perfomance.
You can do
SELECT ((select count(id) from employee where..) + (select count(id) from emplyer where..) + (select count(id) from users where..))
There will hardly be a performance difference - adding three integers is neither a challenge for the MySql nor for the php engine.
If you use group by clause you can add option WITH ROLLUP which will return total sum of all counts in group. See http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html
精彩评论