where子句中的多个列一起

时间:2016-04-26 04:18:41

标签: mysql left-join

我有一个表订阅如下

订阅

 user     user_id     cve_id         vulnerability_id    
 -------  ----------  -------------  ------------------- 
 kumar    17          CVE-2016-3987  74                  
 rajesh   16          CVE-2016-3987  74             

我有另一张表如下

 APP_USER_ID     VULNERABILITY_ID     STATUS    
 --------------  -------------------  --------- 
 16              74                   assigned  

我想要检索到目前为止尚未分配给用户的所有订阅。我尝试了以下查询

select * from subscription where user_id not in (select APP_USER_ID from app_user_subscription )

但如果用户匹配,它会跳过所有订阅。我理解为什么会这样。因为我只提到user_id。我想基于user_idcve_id跳过。我如何更新我的查询?

2 个答案:

答案 0 :(得分:1)

通过在表上使用LEFT OUTER JOIN,将返回所有行,您可以通过使用WHERE子句返回具有空值的行来过滤未分配的行

SELECT a.* FROM user u 
LEFT OUTER JOIN APP_USER_ID a on a.APP_USER_ID = u.user_id
WHERE u.user_id IS NULL

答案 1 :(得分:0)

select * from subscription where (user_id,cve_id) not in (select APP_USER_ID,<corresponding column for cve_id> from app_user_subscription )