这个MySql语句该怎么优化?
表的结构是
日期 号码 金额 状态
2009-11-23 9874 1000 0
需要从表中取出每天总共有多少条记录,状态为0的有几条,总共的金额有多少,我写的MySql语句是这样的
SQL code:
select distinct 日期,(select count(日期) from temptable tt where tt.日期=t.日期),
(select count(日期) from temptable tc where tc.日期=t.日期 and tc.状态=0),
(select sum(金额) from temptable ts where ts.日期=t.日期) from temptable t;
SELECT 日期,COUNT(*),SUM(IF(状态=0,1,0)),SUM(金额) from TT
GROUP BY 日期
SQL code:
select 日期,count(*) as 每天总共有多少条记录,
sum(if(状态=0,1,0)) as 状态为0的有几条,
sum(金额) as 总共的金额
from temptable
gruop by 日期
你自己的语句应该也可以啊。 添加相应索引 (日期,状态) 之后应该就可以了。
SQL code:
select distinct 日期,
(select count(日期) from temptable where 日期=t.日期),
(sele
相关问答:
在三十讲遇到这样一个问题就是运行代码时出现错误 Fatal error: Call to undefined method mysql::fetch_array() in D:\WWW\news\index.php on line 12
,我把mysql::fetch_array() 改成mysql::fetch_row() 又出现F ......
这个视频讲的很详细, 对新手非常有用, 基本上一看就懂
由于太大了(50m, 我只能上传20m), 我上传不了, 只好贴出下载地址
下载地址: http://ftel1.3800hk.com/0807/080720djxnzj.rar
好东西,下个看看
......
SQL code:
create PROCEDURE aa(SqlCMD1 varchar(8000),SqlCMD2 varchar(8000),SqlCMD3 varchar(8000))
begin
declare exit handler for sqlexception rollback;
start TRANSACTION;
EXECUTE SqlCMD1;
EXE ......