日期:2014-05-16  浏览次数:20719 次

跪求 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 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

------解决方案--------------------
你就直接UNION吧。
------解决方案--------------------
直接union all性能会好一些的。
------解决方案--------------------
union all就可以的。