首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

跪求 MySQL 多条语句查询解决方案

2012-06-01 
跪求 MySQL 多条语句查询小弟是菜鸟,如果高手觉得问题简单请不要喷小弟.还望高手解答小弟问题感激不尽问题

跪求 MySQL 多条语句查询
小弟是菜鸟,如果高手觉得问题简单请不要喷小弟.还望高手解答小弟问题 感激不尽

问题如下:
首页要列如下数据
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来写,所以麻烦各位大虾给个解决方案,怎么能一次把这些数据查询出来
由于新注册的账号没什么分,希望不要见怪


[解决办法]
整成一条查询的意思吗?

SQL code
一:select count(*) as 总数 from A .....union allselect count(*) as 有效数 from A where 条件='有效'union allselect count(*) as 总数 from B .....union allselect count(*) as 有效数 from B where 条件='有效'union allselect count(*) as 总数 from C .....union allselect count(*) as 有效数 from C where 条件='有效'union allselect count(*) as 总数 from D .....union allselect count(*) as 有效数 from D where 条件='有效'union allselect count(*) as 总数 from E .....union allselect count(*) as 有效数 from E where 条件='有效'二:select "A", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from Aunion select "B", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from Bunion select "C", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from Cunion select "D", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from Dunion select "E", count(*) as 总数, sum(if(条件='有效', 1, 0) as 有效数 from E
[解决办法]
你就直接UNION吧。
[解决办法]
直接union all性能会好一些的。
[解决办法]
union all就可以的。

热点排行