select  s.name,
sum(if(o.cp>0,1,0)) as c,
sum(if(o.cp=0,1,0)) as h,
sum(o.p) as p,
sum(o.u) as u,
published,s.id 
from table_s as s inner join table_o as o 
on s.middle=o.middle group by s.name where published = 1 ORDER BY s.id asc上面的sql语句 去掉 where published =1 就是对的,加上这个where published =1 就错的。错在哪呢?找半天,找不到,高手指教一下。谢谢

解决方案 »

  1.   

    select  s.name, 
    sum(if(o.cp>0,1,0)) as c, 
    sum(if(o.cp=0,1,0)) as h, 
    sum(o.p) as p, 
    sum(o.u) as u, 
    published,s.id 
    from table_s as s inner join table_o as o 
    on s.middle=o.middle 
    where published = 1  -- where 必须在group by 之前。
    group by s.name 
    ORDER BY s.id asc 
      

  2.   

    文档中有SELECT和其它语句的语法介绍。
    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
      

  3.   

    select  s.name, 
    sum(if(o.cp>0,1,0)) as c, 
    sum(if(o.cp=0,1,0)) as h, 
    sum(o.p) as p, 
    sum(o.u) as u, 
    published,s.id 
    from table_s as s inner join table_o as o 
    on s.middle=o.middle  
    where published = 1 
    group by s.name
    ORDER BY s.id asc 
    group by 要放在where条件之后
      

  4.   

    having
    版主敬业精神不是一天 两天了