admin

SQL:``在选择列表中,'olumn'tbl.column'是无效的,因为它既不包含在聚合函数中也不包含在GROUP BY子句中。''

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语句如下:
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我没有收到以上错误。那么,如何在不出现所示错误的情况下使上面显示的语句起作用?

DBLibrary: Tedious.js


阅读 296

收藏
2021-07-01

共1个答案

admin

您使用了聚合函数,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
2021-07-01