嗨,我需要这样的查询
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
我可以像这样添加这些结果
count = count1+count2+count3;
但我想知道是否有任何选项使用单个查询获取计数,而不是单独添加这些结果。我还想知道就应用程序性能而言哪一种是最好的方法。
最佳答案
你可以做到
SELECT ((select count(id) from employee where..) + (select count(id) from emplyer where..) + (select count(id) from users where..))
几乎不会有性能差异 - 添加三个整数对于 MySql 和 php 引擎来说都不是挑战。
关于php - 如何使用mysql5中的查询添加不同查询的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075664/