表的结构是日期              号码           金额           状态
2009-11-23        9874           1000             0需要从表中取出每天总共有多少条记录,状态为0的有几条,总共的金额有多少,我写的MySql语句是这样的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;

解决方案 »

  1.   

    SELECT 日期,COUNT(*),SUM(IF(状态=0,1,0)),SUM(金额) FROM TT
    GROUP BY 日期
      

  2.   

    select 日期,count(*) as 每天总共有多少条记录,
    sum(if(状态=0,1,0)) as 状态为0的有几条,
    sum(金额) as 总共的金额
    from temptable
    gruop by 日期
      

  3.   

    你自己的语句应该也可以啊。 添加相应索引  (日期,状态) 之后应该就可以了。select distinct 日期,
    (select count(日期) from temptable where 日期=t.日期),
    (select count(日期) from temptable where 日期=t.日期 and 状态=0),
    (select sum(金额) from temptable where 日期=t.日期) 
    from temptable t;
      

  4.   

    sum(if(状态=0,1,0)) 这句是什么意思,为什么会有3个参数?
      

  5.   

    if(状态=0,1,0):
    如果状态=0, 结果为1,否则为0
    再SUM求和
      

  6.   

    IF(expr1,expr2,expr3) If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used. mysql> SELECT IF(1>2,2,3);
            -> 3
    mysql> SELECT IF(1<2,'yes','no');
            -> 'yes'
    mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
            -> 'no'