mysql 不使用汇总的总和

mysql sum totals without using with rollup

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

这不起作用告诉我无法执行查询。 'where 子句

中的未知列 'i.signedupdate'
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 年,但没有专栏摘要

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

如果我简单地删除子查询并保留 'with rollup' 它会提醒我不正确地使用 order by & with rollup?

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

您应该按照建议使用 SQL 视图 here

It's recommended to use a Sql view when the Sql is complex. It's essentially a virtual table, of which that is defined as a SQL SELECT query with joins. Because a database view is similar to a database table, which consists of rows and columns, so you can query data against like an actual database table.

Another benefit is that you can then reuse this view. Additionally, if you use a view instead of a joined tables, in the future if you need to change a column, you can easily do that and only require changes to the Sql view.