mysql总计不使用汇总

时间:2018-01-12 09:53:15

标签: php mysql sum rollup

我使用phpgrid显示查询数据,然后通过页面上的表单选择进行过滤。这个工作正常,直到我添加汇总到查询总和列,我想知道是否有任何其他方法来汇总这些列(其中12个)?

这并不能告诉我无法执行查询。未知专栏' i.signedupdate'在' where子句

SELECT * FROM
  (SELECT IFNULL(c.Adviser, 'GRAND TOTAL') AS Adviser,
  Sum(Month(i.SignedUpDate) = 1) As Jan,
  Sum(Month(i.SignedUpDate) = 2) As Feb,
  Sum(Month(i.SignedUpDate) = 3) As Mar,
  Sum(Month(i.SignedUpDate) = 4) As Apr,
  Sum(Month(i.SignedUpDate) = 5) As May,
  Sum(Month(i.SignedUpDate) = 6) As Jun,
  Sum(Month(i.SignedUpDate) = 7) As Jul,
  Sum(Month(i.SignedUpDate) = 8) As Aug,
  Sum(Month(i.SignedUpDate) = 9) As Sept,
  Sum(Month(i.SignedUpDate) = 10) As Oct,
  Sum(Month(i.SignedUpDate) = 11) As Nov,
  Sum(Month(i.SignedUpDate) = 12) As Dece,
  i.id,
  Count(i.id) As Total
    From
  tbl_lead i Inner Join
  tbl_clients c On i.client_id = c.client_id
Group By
    c.Adviser with rollup) As t

这样可行,我可以选择年份,但没有列摘要

SELECT * FROM
  c.Adviser AS Adviser,
  Sum(Month(i.SignedUpDate) = 1) As Jan,
  Sum(Month(i.SignedUpDate) = 2) As Feb,
  Sum(Month(i.SignedUpDate) = 3) As Mar,
  Sum(Month(i.SignedUpDate) = 4) As Apr,
  Sum(Month(i.SignedUpDate) = 5) As May,
  Sum(Month(i.SignedUpDate) = 6) As Jun,
  Sum(Month(i.SignedUpDate) = 7) As Jul,
  Sum(Month(i.SignedUpDate) = 8) As Aug,
  Sum(Month(i.SignedUpDate) = 9) As Sept,
  Sum(Month(i.SignedUpDate) = 10) As Oct,
  Sum(Month(i.SignedUpDate) = 11) As Nov,
  Sum(Month(i.SignedUpDate) = 12) As Dece,
  i.id,
  Count(i.id) As Total
    From
  tbl_lead i Inner Join
  tbl_clients c On i.client_id = c.client_id
Group By
    c.Adviser

如果我只是删除子查询并将'与汇总'它警告我&的错误使用顺序汇总?

任何想法都非常感激,只需要总结所有这些列

1 个答案:

答案 0 :(得分:1)

您应该使用建议here的SQL视图。

  

建议使用Sql视图   当Sql很复杂时。它本质上是一个虚拟表,其中   它被定义为带有连接的SQL SELECT查询。因为一个数据库   view类似于数据库表,它由行和组成   列,因此您可以像实际数据库表一样查询数据。

     

另一个好处是您可以重复使用此视图。另外,如果   如果需要,将来使用视图而不是连接表   要更改列,您可以轻松地执行此操作,只需要更改   Sql视图。