2013-04-16 150 views
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 = @idSQL:“列'tbl.column'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。”

当我删除聚合函数MAX on tblOther.columnOtherId我没有收到上述错误。那么如何让上面显示的语句在没有显示错误的情况下工作呢?

DBLIBRARY:Tedious.js

回答

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 
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 
+0

由于@JW首先得到了答案,即使你的答案也是正确的,它将被选中为正确答案。 – camelCaseD

相关问题