【问题标题】:Mysql Nested query and GROUP BYMysql 嵌套查询和 GROUP BY
【发布时间】:2014-05-08 01:12:51
【问题描述】:

我正在尝试对我的数据库执行以下查询:-

SELECT
source, Month as t1,
GROUP_CONCAT(SELECT SUM(amount) FROM `reports` GROUP BY Month) as amount
FROM `reports`
GROUP BY source

获取不同来源1个月内获得的资金总额的sourcemonthconcatenated string。但我得到一个语法错误。

【问题讨论】:

  • 是否有 2 个表名为 reportreports 或者是拼写错误?
  • 您正在对GROUP BY 使用有害的非标准 MySQL hackstension。请阅读这个。 dev.mysql.com/doc/refman/5.5/en/group-by-extensions.html
  • @Vatev 他们是一样的
  • @mega6382 那么应该是report 还是reports?实际上,如果您可以编辑您的帖子以使它们成为他们需要的样子,那就太好了。
  • @mega6382 感谢您修复它。 :)

标签: mysql sql nested-queries


【解决方案1】:

我不太确定您需要什么,希望是以下两者之一:

SELECT source, Month, SUM(amount) as sum
FROM reports
GROUP BY source, Month

以上,但按来源分组,总和列在一个字段中:

SELECT source, GROUP_CONCAT(sum) as sums
FROM (
    SELECT source, Month, SUM(amount) as sum
    FROM reports
    GROUP BY source, Month
) as t
GROUP BY source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多