开发者

SQL: Need help creating a one-to-many Set comparing query

The parameters are thus:

  1. I have a table called Tasks with columns ID (primary key) and label (text).
  2. I have another table called Locations with a foreign key referencing Tasks' ID and a name for a location (text).
  3. In my code I have a set of locations.
  4. UPDATED: I need a query to return all tasks that have associated locations found within my set. Each task that has an associated location not found in my set must be t开发者_如何学JAVAhrown out.

What's the best way to go about this?


For the modified requirement:

select * from tasks t
where exists (select null from locations l
              where t.id = l.task_id and l.name in ('London', 'Geneva'...)) and
  not exists (select null from locations l
              where t.id = l.task_id and l.name not in ('London', 'Geneva'...))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜