【问题标题】:SQL: "Column 'tbl.column' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."SQL:“列 'tbl.column' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。”
【发布时间】:2013-04-16 02:33:00
【问题描述】:


我想知道为什么我会收到以下错误:
Column 'tbl.column' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
当 sql 语句看起来像:
SELECT tbl.column, MAX(tblOther.columnOtherId) AS otherID FROM (tbl INNER JOIN tblOther ON tbl.columnId = tblOther.columnOtherId) INNER JOIN tblOtherAgain ON tblOther.columnOtherId = tblOtherAgain.columnAgainOtherId WHERE tblOther.columnOtherAgainId = @id

当我删除 tblOther.columnOtherId 上的聚合函数 MAX 时,我没有收到上述错误。那么如何让上面显示的语句正常工作而不出现显示的错误呢?

DBLibrary:Tedious.js

【问题讨论】:

    标签: sql sql-server node.js


    【解决方案1】:

    您使用了聚合函数MAX(),并且SELECT 子句中有一个字段未聚合,这就是您需要GROUP BY 子句的原因,

    SELECT  tbl.column, 
            MAX(tblOther.columnOtherId) AS otherID 
    FROM    (tbl INNER JOIN tblOther 
                ON tbl.columnId = tblOther.columnOtherId) 
            INNER JOIN tblOtherAgain 
                ON tblOther.columnOtherId = tblOtherAgain.SourceId 
    WHERE   tblOther.columnOtherAgainId = @id
    GROUP   BY tbl.column
    

    【讨论】:

      【解决方案2】:
      SELECT tbl.column, MAX(tblOther.columnOtherId) AS otherID FROM (tbl INNER JOIN tblOther ON tbl.columnId = tblOther.columnOtherId) INNER JOIN tblOtherAgain ON tblOther.columnOtherId = tblOtherAgain.columnAgainOtherId WHERE tblOther.columnOtherAgainId = @id
      GROUP BY tbl.column
      

      【讨论】:

      • 由于@JW 首先得到了答案,即使你的也是正确的,他也会被选为正确的。
      猜你喜欢
      • 2011-07-04
      • 2013-08-17
      • 2011-05-22
      相关资源
      最近更新 更多