简体   繁体   English

mysql总和不与汇总一起使用

[英]mysql sum totals without using with rollup

I am using phpgrid to display query data, then filtering through form select on page. 我正在使用phpgrid显示查询数据,然后通过页面上的表单选择进行过滤。 This works fine until I add with rollup to the query to sum columns and I wondered if there is any other method of summing these columns (12 of them)? 直到我将汇总添加到查询中以对列求和,并且我想知道是否还有其他方法对这些列求和(其中的12个)时,此方法工作正常。

This doesn't work telling me Couldn't execute query. 这对告诉我无法执行查询无效。 Unknown column 'i.signedupdate' in 'where clause “ 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

This works, I can select years ok but no column summary 这行得通,我可以选择年份,但没有专栏摘要

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

If I simply remove the subquery and leave 'with rollup' it alerts me of incorrect use of order by & with rollup? 如果我只是删除了子查询并保留了“ with rollup”,它会提醒我&和rollup不正确地使用了订单?

Any ideas much appreciated, just need to sum all those columns 非常感谢任何想法,只需将所有这些列加起来

You should use a SQL view as recommended here . 您应该按照此处的建议使用SQL视图。

It's recommended to use a Sql view when the Sql is complex. 当Sql很复杂时,建议使用Sql视图。 It's essentially a virtual table, of which that is defined as a SQL SELECT query with joins. 它本质上是一个虚拟表,其中的虚拟表定义为具有联接的SQL SELECT查询。 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. 此外,如果使用视图而不是联接表,则将来如果需要更改列,则可以轻松地做到这一点,并且只需要更改Sql视图即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM