-1

团队成员、用户表和积分表

起初需要获取所有不在 team_members 表中的用户

我做了这个

SELECT UserID
FROM usertable
WHERE UserID NOT 
IN (SELECT UserID FROM team_members)
AND Location =  'my loctaion'

这很好用

但现在我需要根据用户表中的用户 ID 从点表中获取一些信息

SELECT usertable.* , pointstable.UserID, SUM( pointstable.RemainderPoints ) 
AS points FROM   usertable LEFT JOIN pointstable ON usertable.UserID = pointstable.UserID
WHERE  usertable.UserID NOT IN (SELECT usertable.UserID FROM team_members)
GROUP BY usertable.UserID 

这不会返回任何东西

有任何想法吗?

4

3 回答 3

1

请注意,您的原始查询可以重写如下:

SELECT UserID
  FROM usertable u
  LEFT
  JOIN team_members tm
    ON tm.userid = u.userid
 WHERE Location =  'my location'
   AND tm.userid IS NULL;
于 2013-10-09T16:13:37.787 回答
0

试试下面的一个:

select users.UserID,Pointstable.UserID,SUM(remainderpoints) from Users
left join  Pointstable
 on users.UserID=Pointstable.UserID
where users.userid not in (SELECT usertable.UserID FROM team_members)
  group by users.UserID, pointstable.UserID

如果您需要选择部分中用户表中的更多列,请将其添加到分组依据。

于 2013-10-09T16:37:29.597 回答
0

拿走了你所做的……以防万一其他人对此有任何问题

 SELECT usertable.*,pointstable.UserID, SUM( pointstable.RemainderPoints ) AS points ,team_members.* 
FROM usertable LEFT JOIN pointstable ON usertable.UserID = pointstable.UserID
LEFT JOIN team_members ON pointstable.UserID = team_members.UserID 
WHERE usertable.Location =  'South Molton'
AND team_members.UserID IS NULL GROUP BY usertable.UserID
于 2013-10-09T16:39:56.103 回答