我想知道为什么会出现以下错误: 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。
Column 'tbl.column' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
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
当我删除聚合函数 MAX时 ,tblOther.columnOtherId我没有收到以上错误。那么,如何在不出现所示错误的情况下使上面显示的语句起作用?
tblOther.columnOtherId
DBLibrary: Tedious.js
您使用了聚合函数,MAX()并且SELECT子句中有一个未聚合的字段,这就是为什么您需要有GROUP BY子句的原因,
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