求一个统计数据库 所有表的记录条数的总和的语句(mysql)

求一个统计数据库 所有表的记录条数的总和的语句(mysql),第1张

<php

mysql_connect('localhost','数据库帐号','数据库密码');

mysql_select_db('数据库名');

$result = mysql_query('show tables'); //获取所有表名

$all_record = 0;

while($record = mysql_fetch_row($result)) {

$tb_name = $record[0]; //表名

$n = mysql_result(mysql_query('select count() from `'$tb_name'`'),0);

$all_record+=$n;

}

echo $all_record; //总记录数

>

select count(xfjh) from 表名 where xfjh=1

这是得到了这个表中一个有多少行数据满足xfjh的值为1,

SQL中常用的就五个聚合函数

SUM()求和,只能对数字用,

AVG()求平均数,对数字用

MIN()求最小值

MAX()求最大值 可以对数字,日期,可以比较大小的列用

COUNT()统计,

用这些的时候一般情况都跟GROUP BY (分组)一起用。

create table aa(

typeId int,

name varchar(20))

create table bb(

id  int ,

name  varchar(20),

typeId int ,

num int ,

m int 

)

insert into aa values(1,'自行车'),(2,'摩托车'),(3,'小汽车'),(4,'其它')

insert into bb values

(1,'li',1,1,500),

(2,'li',2,2,5000),

(3,'li',3,1,50000),

(4,'wanger',1,1,450),

(5,'wanger',2,1,5600)

select bbname,

 sum(case  when aaname ='自行车' then   bbnum

  else 0 

  end ) as [自行车num],

   sum(case  when aaname ='自行车' then   bbm

  else 0 

  end ) as [自行车m],

 

 

 sum(case  when aaname ='摩托车' then   bbnum

  else 0 

  end ) as [摩托车num],

   sum(case  when aaname ='摩托车' then   bbm

  else 0 

  end ) as [摩托车m],

  

  

  sum(case  when aaname ='小汽车' then   bbnum

  else 0 

  end ) as [小汽车num],

   sum(case  when aaname ='小汽车' then   bbm

  else 0 

  end ) as [小汽车m],

  

  

  sum(case  when aaname ='其它' then   bbnum

  else 0 

  end ) as [其它num],

   sum(case  when aaname ='其它' then   bbm

  else 0 

  end ) as [其它m] ,

  

   sum(bbnum) as [Allnum],

   sum(bbm ) as [Allm] 

 from aa right  join bb on aatypeId=bbtypeId

group by bbname 

truncate table aa

drop table aa

truncate table bb

drop table bb

 

 go

mysql 脚本的方法,请对照这个 sql server脚本去修改,如有疑问,及时沟通

select c_sch, c_year ,c_reg ,c_code ,c_weave,count() as 总和 from j_cor表名 where c_sch='012' and c_year in (2002,2003,2004,2005,2006,2007) and c_code='' and c_weave like ‘333%’ group by c_sch, c_year ,c_reg ,c_code ,c_weave

你自己照葫芦画瓢也画出来了,都写这么多了你还问

以上就是关于求一个统计数据库 所有表的记录条数的总和的语句(mysql)全部的内容,包括:求一个统计数据库 所有表的记录条数的总和的语句(mysql)、求SQL统计计语句、mysql数据库的这个数据统计sql语句怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10195053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存