SQL检查组中是否存在值

时间:2016-10-13 18:25:20

标签: mysql sql

说我有一个频道成员表

channelID  | accountID  | role
------------------------  
1          | Bob   | 100
2          | Bob   | 100
1          | Dave  | 101

和另一个频道表

channelID  | channelName     | type 
---------------------------------------  
1          | Public Channel  | public
2          | Private Channel | private

我如何获取用户不在的频道?

我试图这样做,但无法弄清楚如何对频道成员表进行分组,因为有两个使用channelID 1的项目,所以当我查询用户Dave时,如果我没有返回任何内容,我会得到频道1。我正在使用的查询:

Select ChatChannels.channelID, channelName, type
From ChatChannels
INNER JOIN ChatChannelMembers
On ChatChannels.channelID = ChatChannelMembers.channelID
Where AccountID = 'Dave'

1 个答案:

答案 0 :(得分:2)

这有用吗?

SELECT (stuff)
FROM ChatChannels CC
WHERE CC.type = 'Public'
  AND CC.channelID NOT IN (SELECT channelID FROM ChatChannelMembers WHERE AccountID = 'Dave')