2016-04-30 349 views
0

我試圖讓我的柱使用具有等於過濾器包含的,但問題是,它給出了這樣的錯誤消息:列在HAVING子句中無效,因爲它不是在聚合函數或GROUP BY

column is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY 

,當我嘗試添加過濾器的選擇和組由它說:

Column names in each view or function must be unique 

,因爲無論是列和過濾器是相同的:

列:

 userTable.userid 

過濾器:

user_filter.userid 

這裏是我的代碼:

SELECT DISTINCT 
userId, 
username, 
sum(userItem) 
from userTable inner join user_filter 
on userTable.userName = user_filter.userName 
group by userId, 
username, 
having userTable.userId = user_filter.userId 
+2

編輯您的問題幷包含生成錯誤的查詢。 –

+0

好吧,我會添加查詢 – abdu

+0

你究竟在努力實現什麼?當兩個表都有'userid'時,爲什麼你用'username'加入?爲什麼userfilter在用戶名已經在'usertable'中時有'username'? –

回答

0

你不需要Having條款添加另一過濾器Where條款

SELECT userid, 
     username, 
     Sum(useritem) 
FROM usertable 
     INNER JOIN user_filter 
       ON usertable.username = user_filter.username 
        AND usertable.userid = user_filter.userid 
GROUP BY userid, 
      username 

也是DISTINCT是多餘的,因爲Group by用於select

相關問題