mysql-如何在一个sql查询中计算来自多个表的行

前端之家收集整理的这篇文章主要介绍了mysql-如何在一个sql查询中计算来自多个表的行 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个问题,必须从多个表中获取行计数.

例如:

SELECT COUNT(*) FROM table1;
SELECT COUNT(*) FROM table1 where condition;
SELECT COUNT(*) FROM table2;
SELECT COUNT(*) FROM table3 where condition;
@H_403_9@
最佳答案
您可以使用UNION

SELECT COUNT(*) FROM table1
UNION
SELECT COUNT(*) FROM table1 where condition;
UNION
SELECT COUNT(*) FROM table2;
UNION
SELECT COUNT(*) FROM table3 where condition;
@H_403_9@

您可以添加额外的字段,以通过以下查询找到要获得的行

SELECT "COND-1" AS TITLE,COUNT(*) FROM table1
UNION
SELECT "COND-2" AS TITLE,COUNT(*) FROM table1 where condition;
UNION
SELECT "COND-3" AS TITLE,COUNT(*) FROM table2;
UNION
SELECT "COND-4" AS TITLE,COUNT(*) FROM table3 where condition;
@H_403_9@

猜你在找的MySQL相关文章