假设我有两张table:人:
id | name --------- 1 | John 2 | Jack 3 | Pepe
工作与人际关系:
job | person ------------ 15 | 1 16 | 1 15 | 2 16 | 3
我想选择所有拥有15号和16号工作的人,但不选择只有15号或16号工作的人。意思是我想接待约翰。我如何做到这一点?
icomxhvb1#
这应该起作用:
select p.id, p.name from people p join jobs_people_relations r on p.id = r.person where r.job in(15,16) group by id, name having count(*) = 2
fdx2calv2#
您的示例似乎有点模糊,但是在t-sql中您可以尝试这样做
Select Distinct name From people Inner Join jobs_people_relations On people.id = jobs_people_relations.person Where people.id = 1
yvt65v4c3#
你可以做一个 Group By 关于人的细节。在中使用条件聚合 Having 过滤掉某人 job = 15 和 job = 16 .请尝试以下操作:
Group By
Having
job = 15
job = 16
SELECT p.id, p.name FROM people AS p JOIN jobs_people_relations AS jpr ON jpr.person = p.id GROUP BY p.id, p.name HAVING SUM(jpr.job = 15) AND SUM(jpr.job = 16)
3条答案
按热度按时间icomxhvb1#
这应该起作用:
fdx2calv2#
您的示例似乎有点模糊,但是在t-sql中您可以尝试这样做
yvt65v4c3#
你可以做一个
Group By
关于人的细节。在中使用条件聚合
Having
过滤掉某人job = 15
和job = 16
.请尝试以下操作: