2

如何避免消除零会议的用户?我知道有类似的问题,但是这段代码要复杂得多。

SELECT user.userID, user.contactName, user.email, COUNT( * ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
WHERE user.userID NOT 
IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 ) 
AND SUBSTRING( meeting.meetingCode, 5, 2 ) 
BETWEEN 12 
AND 22 
AND SUBSTRING( meeting.meetingCode, 7, 2 ) 
BETWEEN 01 
AND 12 
AND SUBSTRING( meeting.meetingCode, 9, 2 ) 
BETWEEN 01 
AND 31 
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC
4

1 回答 1

5

您需要将会议代码表的逻辑放入您的联接中。否则,与您从会议表中过滤掉的记录匹配的用户将从您的结果中过滤掉。使您的 JOIN 本质上是一个 INNER 连接。我认为您还应该在 BETWEEN 子句中的值周围加上单引号。

SELECT  user.userID, user.contactName, user.email, COUNT( meeting.userID ) AS meetingsCount

FROM    user

        LEFT OUTER JOIN meeting ON user.userID = meeting.userID
        AND SUBSTRING( meeting.meetingCode, 5, 2 ) BETWEEN '12' AND '22'
        AND SUBSTRING( meeting.meetingCode, 7, 2 ) BETWEEN '01' AND '12'
        AND SUBSTRING( meeting.meetingCode, 9, 2 ) BETWEEN '01' AND '31' 

WHERE user.userID NOT IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 ) 

GROUP BY user.userID, contactName, email

ORDER BY meetingsCount DESC
于 2013-02-19T16:33:55.507 回答