每个派生表必须有自己的别名(

时间:2012-04-03 11:48:30

标签: mysql

 SELECT * FROM 
   (SELECT sum(DATA.dayMB) as "totaldayMb", 
           data.customerid 
    FROM DATA,customer 
    WHERE data.customerid=customer.customerid 
          AND customer.projectid='"+ ProjectID +"' 
          AND customer.stateid='"+ StateID +"' 
          AND DATA.dataDate between '"+ initialDate +"' 
          AND '"+ finalDate +"' 
          AND data.upstatus='"+upstatus+"' 
    GROUP BY data.customerid 
    ORDER BY sum(DATA.dayMB) desc
   ) 
  WHERE ROWNUM <= 25 

在此查询中,我想选择25个用户,他们使用最大数据b / w dis日期。我正在使用mysql。

2 个答案:

答案 0 :(得分:1)

SELECT * FROM 
   (SELECT sum(DATA.dayMB) as "totaldayMb", 
           data.customerid 
    FROM DATA,customer 
    WHERE data.customerid=customer.customerid 
          AND customer.projectid='"+ ProjectID +"' 
          AND customer.stateid='"+ StateID +"' 
          AND DATA.dataDate between '"+ initialDate +"' 
          AND '"+ finalDate +"' 
          AND data.upstatus='"+upstatus+"' 
    GROUP BY data.customerid 
    ORDER BY sum(DATA.dayMB) desc
   ) AS baseview
  LIMIT 25 

在最后一行中注意AS baseview

当然,只有当内部查询在查询缓存中时才有意义。如果没有,请使用

SELECT sum(DATA.dayMB) as "totaldayMb", 
           data.customerid 
FROM DATA,customer 
WHERE data.customerid=customer.customerid 
          AND customer.projectid='"+ ProjectID +"' 
          AND customer.stateid='"+ StateID +"' 
          AND DATA.dataDate between '"+ initialDate +"' 
          AND '"+ finalDate +"' 
          AND data.upstatus='"+upstatus+"' 
GROUP BY data.customerid 
ORDER BY sum(DATA.dayMB) desc
LIMIT 25

答案 1 :(得分:0)

使用LIMIT运算符(docu @ MySQL)限制返回的行数,而不是围绕它添加其他查询。

这样你也可以避免错误。

SELECT sum(DATA.dayMB) as "totaldayMb", 
       data.customerid 
FROM DATA,customer 
WHERE data.customerid=customer.customerid 
      AND customer.projectid='"+ ProjectID +"' 
      AND customer.stateid='"+ StateID +"' 
      AND DATA.dataDate between '"+ initialDate +"' 
      AND '"+ finalDate +"' 
      AND data.upstatus='"+upstatus+"' 
GROUP BY data.customerid 
ORDER BY sum(DATA.dayMB) desc
LIMIT 0,25