我想知道为什么会出现以下错误:
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

===============>>#1 票数:0 已采纳

您使用了聚合函数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 票数:0

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

  ask by camelCaseD translate from so

未解决问题?本站智能推荐: