Access SQL using IN where record must satisfy all values

我的未来我决定 提交于 2019-12-25 08:16:44

问题


I have a join statement, like this:

SELECT 
distinct(p.id), p.id, p.first_name as [First Name], p.last_name as [Last Name] 
FROM  tbl_person as p , 
tbl_person_languages as pl
WHERE  
pl.person_id = p.id  AND 
pl.language_id in (12,14,...)

but this returns all records. I would like to pull back the distinct record of an individual that has all values of pl.language_id, not just one or more values. I should note, the values in the IN statement may have 1 or more values

Any ideas on how to do this? I've tried changing the IN statement to something like this:

 WHERE  
    pl.person_id = p.id  AND 
    pl.language_id =12 AND
    pl.language_id = 14

but this returns nothing.

thanks for any help


回答1:


You have to group by and count:

SELECT 
     p.id, p.first_name as [First Name], p.last_name as [Last Name] 
FROM  tbl_person as p , 
tbl_person_languages as pl
WHERE  
pl.person_id = p.id  AND 
pl.language_id in (12,14,...)
GROUP BY p.id, p.first_name, p.last_name
HAVING COUNT(DISTINCT pl.language_id) = 2   -- 12 and 14


来源:https://stackoverflow.com/questions/40980610/access-sql-using-in-where-record-must-satisfy-all-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!