mysql 统计多表
------------------------------------------------------------------------------------------- select * from ((select count(*) as countnum from a_admin_code where classid=6) a,(select count(*) as countnum from a_admin_code_class where fid=1) b,(select count(*) as countnum from a_admin_company)c ) countnum countnum countnum 3 5 1 select a.countnum+b.countnum+c.countnum as allnum from ((select count(*) as countnum from a_admin_code where classid=6) a,(select count(*) as countnum from a_admin_code_class where fid=1) b,(select count(*) as countnum from a_admin_company)c ) allnum 9 统计一张表------------------------------------------------------------------------------------------- SELECT a.classid,count(*) as tjnum,b.classname,b.id FROM `a_admin_code` as a left join a_admin_code_class as b on a.classid=b.id group by classid ------------------------------------------------------------------------------------------- union all统计多表 现在需要把它们汇总加起来,可以使用如下方法实现 SELECT COUNT(cnt) FROM ( SELECT COUNT(id) AS cnt FROM test_0 WHERE field1 = 1 UNION ALL SELECT COUNT(id) AS cnt FROM test_1 WHERE field1 = 1 . . . UNION ALL SELECT COUNT(id) AS cnt FROM test9 WHERE field1 = 1 ) AS countdata 注:为UNION ALL的所有表做一个别名是必须的,虽然这个别名并没有什么意义 但如果没有的话mysql会报出错误:Every derived table must have its own alias ------------------------------------------------------------------------------------------- mysql count多表数据总和 SELECT a.countNum + b.countNum + c.countNum FROM ( (SELECT COUNT(*) AS countNum FROM table_a) a, (SELECT COUNT(*) AS countNum FROM table_b) b, (SELECT COUNT(*) AS countNum FROM table_c) c )
mysql count 统计多表数据
继续阅读与本文标签相同的文章
上一篇 :
js常用验证
下一篇 :
layer多组、分组图片相册预览
-
阿里云Linux服务器动态扩容(阿里云服务器扩容后磁盘空间无变化解决方法)
2020-11-30栏目: 建站教程
-
6个SEM实战技巧,大神手把手教你做推广!
2020-07-20栏目: SEM
-
苏州美食推荐,持续更新
2020-06-17栏目: 生活笔记
-
企业如何玩转视频号
2020-05-12栏目: 运营杂谈
-
SEO关键词排名该掌握的核心优化技巧
2020-05-12栏目: SEO
打印
分享
发表评论 已发布 0 条