1


我想知道为什么我会收到以下错误:
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:繁琐的.js

4

2 回答 2

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
于 2013-04-16T02:34:59.110 回答
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
于 2013-04-16T02:35:13.373 回答