跪求 MySQL 多条语句查询

CaiNiaoQiuJieY 2012-05-25 01:07:16
小弟是菜鸟,如果高手觉得问题简单请不要喷小弟.还望高手解答小弟问题 感激不尽

问题如下:
首页要列如下数据
A:(总数/有效数)B:(总数/有效数)C:(总数/有效数)D:(总数/有效数)E:(总数/有效数)

总数只查询当前数据库中最近一个星期的

要得到这些数据就有如下SQL
select count(*) from A .....
select count(*) from A where 条件='有效'

select count(*) from B .....
select count(*) from B where 条件='有效'

select count(*) from C .....
select count(*) from C where 条件='有效'

select count(*) from D .....
select count(*) from D where 条件='有效'

select count(*) from E .....
select count(*) from E where 条件='有效'


由于考虑性能所以不能分这么多SQL来写,所以麻烦各位大虾给个解决方案,怎么能一次把这些数据查询出来
由于新注册的账号没什么分,希望不要见怪
...全文
182 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小小小小蜗牛 2012-05-29
  • 打赏
  • 举报
回复
还要什么好的建议啊 直接查出来了不好么? 还想玩什么花样啊
一步步来吧
longzhigu126 2012-05-27
  • 打赏
  • 举报
回复
使用第三方工具,更省心
Rotel-刘志东 2012-05-26
  • 打赏
  • 举报
回复
union all就可以的。
Rotel-刘志东 2012-05-26
  • 打赏
  • 举报
回复
直接union all性能会好一些的。
CaiNiaoQiuJieY 2012-05-26
  • 打赏
  • 举报
回复
难道没有更好的建议了吗?
难道没有更好的建议了吗?
难道没有更好的建议了吗?
难道没有更好的建议了吗?
难道没有更好的建议了吗?
难道没有更好的建议了吗?
CaiNiaoQiuJieY 2012-05-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
分这么多写 和 用union合并起来写 没什么性能差别
[/Quote]

大虾,你又什么好的建议吗?想要得到这些结果有什么好的解决方法吗?
rucypli 2012-05-25
  • 打赏
  • 举报
回复
分这么多写 和 用union合并起来写 没什么性能差别
ACMAIN_CHM 2012-05-25
  • 打赏
  • 举报
回复
你就直接UNION吧。
nicenight 2012-05-25
  • 打赏
  • 举报
回复
整成一条查询的意思吗?
一:
select count(*) as 总数 from A .....
union all
select count(*) as 有效数 from A where 条件='有效'
union all
select count(*) as 总数 from B .....
union all
select count(*) as 有效数 from B where 条件='有效'
union all
select count(*) as 总数 from C .....
union all
select count(*) as 有效数 from C where 条件='有效'
union all
select count(*) as 总数 from D .....
union all
select count(*) as 有效数 from D where 条件='有效'
union all
select count(*) as 总数 from E .....
union all
select count(*) as 有效数 from E where 条件='有效'

二:
select "A", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from A
union
select "B", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from B
union
select "C", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from C
union
select "D", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from D
union
select "E", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from E

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧