开发者

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

开发者 https://www.devze.com 2023-01-17 01:56 出处:网络
The parameters are thus: I have a table called Tasks with columns ID (primary key) and label (text). I have another table called Locations with a foreign key referencing Tasks\' ID and a name for a

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

精彩评论

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