mysql count 好几个表_来自多个表的MySQL count(*)?

要针对多个表实现此目的,请使用UNION ALL。

语法如下select sum(variableName.aliasName)

from

(

select count(*) as yourAliasName from yourTableName1

UNION ALL

select count(*) as yourAliasName from yourTableName2

) yourVariableName;

让我们实现以上语法。在这里,我正在使用具有更多表的示例数据库。

我们正在使用的两个表是用户演示

在哪里

这是显示两个表的所有记录的查询。查询如下所示以显示表“ userdemo”中的记录。mysql> select *from userdemo;

以下是输出+--------+----------+------------------+

| UserId | UserName | RegisteredCourse |

+--------+----------+------------------+

| 1      | John     | Java             |

| 2      | Larry    | C                |

| 3      | Carol    | C++              |

| 4      | Mike     | C#               |

+--------+----------+------------------+

4 rows in set (0.08 sec)

查询如下所示以显示表“ wheredemo”中的记录。mysql> select *from wheredemo;

以下是输出+------+---------+

| Id   | Name    |

+------+---------+

| 101  | Maxwell |

| 110  | David   |

| 1000 | Carol   |

| 1100 | Bob     |

| 115  | Sam     |

+------+---------+

5 rows in set (0.20 sec)

这是从上述两个表中实现count(*)的查询mysql> select sum(tbl.EachTableCount)

-> from

-> (

-> select count(*) as EachTableCount from userdemo

-> UNION ALL

-> select count(*) as EachTableCount from wheredemo

-> )tbl;

以下是输出+-------------------------+

| sum(tbl.EachTableCount) |

+-------------------------+

| 9                       |

+-------------------------+

1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值