gpt4 book ai didi

mySQL 联合计算行

转载 作者:行者123 更新时间:2023-11-29 04:47:01 24 4
gpt4 key购买 nike

我有一个类似的查询:

select 'table_1', count(*)
from table_1
union
select 'table_2', count(*)
from table_2
union
select 'table_n', count(*)
from table_n

返回每个表(n 个表)的总行数。

  table_1 | 100
table_2 | 150
table_n | 400

我想知道是否有一个 mysql 函数可以在末尾添加一条新记录,使所有行的总和如下:

  table_1 | 100
table_2 | 150
table_n | 400
total | 650

有没有办法在不使用过程的情况下在 mySQL(5.5 版)中做到这一点?(例如,如果支持,则在 sql 中使用变量)

最佳答案

     select ifnull(table_name,'Total'), sum(row_count)
from (select 'table_1' table_name, count(*) row_count
from table_1
union
select 'table_2' table_name, count(*) row_count
from table_2
union
select 'table_n' table_name, count(*) row_count
from table_n ) temp
group by table_name with rollup;

关于mySQL 联合计算行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17525546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com