如何在一个查询中获取2个不同表(和数据库)的行数?

发布于 2024-12-26 17:56:48 字数 327 浏览 0 评论 0原文

我得到了一个名为 accounts 的数据库和该数据库内的 account 表,此外我还得到了名为 playersplayer 的数据库> 该数据库内的表。

如何在一个查询中获取这两个表的行数?

我已经尝试过这个:

SELECT
    SUM(`account`.`account`.`id`) AS 'accounts',
    SUM(`player`.`player`) AS 'players';

但它不起作用。

I got a database named accounts and account table inside of this database, Also I got the database named players and player table inside of this database.

How can I get a rows count of this two tables in one query?

I've tried this:

SELECT
    SUM(`account`.`account`.`id`) AS 'accounts',
    SUM(`player`.`player`) AS 'players';

But it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

誰認得朕 2025-01-02 17:56:48

如果您需要精确的行数(而不是总和),请执行以下操作:

select
(select count(*) from accounts.account) as count1,
(select count(*) from players.player) as count2

select count(*) as `count`,"account" as `table` from accounts.account
union all
select count(*) as `count`,"player" as `table` from players.player

If you need exactly rows count (not sum), than do something like this:

select
(select count(*) from accounts.account) as count1,
(select count(*) from players.player) as count2

or

select count(*) as `count`,"account" as `table` from accounts.account
union all
select count(*) as `count`,"player" as `table` from players.player
-黛色若梦 2025-01-02 17:56:48

对两个 select 语句进行简单的 UNION 操作即可:

SELECT COUNT(*), 'Accounts' FROM Accounts.Account
UNION
SELECT COUNT(*), 'Players' FROM Players.Player

并且您必须使用数据库名称来限定每个表,因为它们位于不同的数据库中。

A simple UNION operation on two select statements will do:

SELECT COUNT(*), 'Accounts' FROM Accounts.Account
UNION
SELECT COUNT(*), 'Players' FROM Players.Player

And you have to qualify each table with the database name since they're in separate databases.

月下伊人醉 2025-01-02 17:56:48

尝试:

SELECT
   COUNT(`account`.`id`) AS 'accounts',
   COUNT(`player`.`player`) AS 'players'
FROM
   `account`,
   `player`

Try:

SELECT
   COUNT(`account`.`id`) AS 'accounts',
   COUNT(`player`.`player`) AS 'players'
FROM
   `account`,
   `player`
似最初 2025-01-02 17:56:48
SELECT COUNT(*) 
FROM (
    SELECT Id 
    FROM accounts.account 
    UNION ALL 
    SELECT player 
    FROM players.player ) AS BothTables
SELECT COUNT(*) 
FROM (
    SELECT Id 
    FROM accounts.account 
    UNION ALL 
    SELECT player 
    FROM players.player ) AS BothTables
罗罗贝儿 2025-01-02 17:56:48
with Value (nbr, name ) as
(
select count(*) amount, 'AccountsCount' as ab from accounts..account
union all
select count(*) amount, 'PlayersCount' as ab from players..player
)
select * 
from value as s
PIVOT(sum(nbr) for name in (AccountsCount, PlayersCount) ) as pvt
with Value (nbr, name ) as
(
select count(*) amount, 'AccountsCount' as ab from accounts..account
union all
select count(*) amount, 'PlayersCount' as ab from players..player
)
select * 
from value as s
PIVOT(sum(nbr) for name in (AccountsCount, PlayersCount) ) as pvt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文