php - 如何使用mysql5中的查询添加不同查询的计数?

标签 php mysql performance mysql5

嗨,我需要这样的查询


    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/

相关文章:

php - 使用 PHP/MySql 中的单个查询从多个表中获取 'related' 数据?

javascript - 是否可以从谷歌驱动器获取特定文件夹的所有文件?

php - 使用 PHP 简单 HTML dom 解析器搜索元素名称

mysql - 在 MySQL 的表名中使用点是一种好习惯吗

mysql - 简单的 MySQL INSERT 错误

java - 从 Hibernate 3 迁移到 4 会减慢启动速度

php - 从mysql的两个表中检索数据

MySQL:ORDER BY 或 GROUP BY 慢?

java - Long primitive 还是 AtomicLong 作为计数器?

performance - 与 Internet 相比,企业网络中的 Azure Web 应用程序性能较差