开发者

SQL - Limiting to one row for matching results

Considering the tables below, how would I write a query that returns profession.profession when the profession.profession_id is present in contractor_has_profession.profession_id, but limiting it to one result for each profession.profession

So in this example the result would be [Coder, Database, Frontend]

contractor_has_profession
contractor_id | profession_id
1开发者_如何学运维             | 5
2             | 5
3             | 5
4             | 2
5             | 1

profession
profession_id | profession
1             | Frontend
2             | Database
3             | Graphics
4             | Sound
5             | Coder


SELECT p.profession
FROM   profession p
WHERE  EXISTS(SELECT *
              FROM   contractor_has_profession c
              WHERE  c.profession_id = p.profession_id)  


Hmm, this should be sufficient:

select distinct p.profession
from profession p
inner join contractor_has_profession c
where p.profession_id = c.profession_id

or if I'm wrong here, then try:

select p.profession
from profession p
inner join contractor_has_profession c
where p.profession_id = c.profession_id
group by p.profession
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜