select 
max(product.productId) as maxProductId,
count(product.productId) as countProductId,
company.userGrade as userGrade,
company.name as companyName
from product left join company on company.companyId=product.companyId
where product.status=3
and match(product.name,product.keywords) against('+light' in boolean mode) 
and product.address='zhejiang'
group by product.companyId
order by (company.userGrade ) desc
,(match(product.name,product.keywords) against('+light' in boolean mode) ) desc
limit 0,20
这样的一段sql语句,那我count明显不能用了,但我还是要统计一下数量用于分页,怎么办

解决方案 »

  1.   

    select count(*) from (
    select
    max(product.productId) as maxProductId,
    count(product.productId) as countProductId,
    company.userGrade as userGrade,
    company.name as companyName
    from product left join company on company.companyId=product.companyId
    where product.status=3
    and match(product.name,product.keywords) against('+light' in boolean mode)
    and product.address='zhejiang'
    group by product.companyId
    order by (company.userGrade ) desc
    ,(match(product.name,product.keywords) against('+light' in boolean mode) ) desc
    limit 0,20
    ) as ttt
      

  2.   

    这个我知道,问题是我limit 0,20,不管怎么样出来的都是20吧
    但我想要整个
      

  3.   

    说真的,感觉不知道你到底想干什么。
    select 
    max(product.productId) as maxProductId,
    count(*) as countcompanyId,
    company.userGrade as userGrade,
    company.name as companyName
    from product left join company on company.companyId=product.companyId
    where product.status=3
    and match(product.name,product.keywords) against('+light' in boolean mode) 
    and product.address='zhejiang'
    group by product.companyId
    order by (company.userGrade ) desc
    ,(match(product.name,product.keywords) against('+light' in boolean mode) ) desc是想这样子吗?
    按照 companyId 分组,并把所有相同的 companyId 数量统计出来。
      

  4.   

    我是想把,group by 以后,一共有几条记录全部统计出来,为的是实现分页
    但limit 0,20又不想不带,想一次性全部取出来
    不过看起来是没办法,总归要统计一次,哎,先这样办法,我一会来结贴
      

  5.   

    给你个简单例子:
    select mycol1,count(*) as col1Count from myTable where 1 group by mycol1;
    显示的就是每个mycol1 数据,并统计出每个mycol1数据的重复数目。如果后面有Limit X,Y 则显示第X到Y条mycol1 数据,并统计出这些mycol1数据的重复数目。
    如果select mycol1,count(*) as col1Count ,NewCol2  ......
    则对 NewCol2  ,之输出第一条符合条件的 NewCol2 数据。
      

  6.   

    SELECT COUNT(DISTINCT product.companyId)
    下面的条件用你的。这样就能统计出来了。