一个查询仅将值与两个条件中的一个匹配,一个查询将值与两个条件匹配

One query that matches values with only one condition out of two, one query that matches values with both conditions

我对如何在 SQL 中执行此操作一无所知。

考虑 R 中的这个表示

set.seed(123)
data.frame(ID = (sample(c(1:5), 10, replace = T)),
       status = (sample(c("yes", "no"), 10, replace = T)),
       amount = (sample(seq(1,50,0.01),10)))

给出了这个 table

   ID status amount
1   3     no  29.87
2   3    yes  26.66
3   2    yes  15.49
4   2    yes  18.89
5   3    yes  44.06
6   5     no  30.79
7   4    yes  17.13
8   1    yes   6.54
9   2    yes  45.68
10  3    yes  12.66

我需要找到两个 SQL 查询。

其中我 select ID 的状态仅为 'NO' 意思是 ID 5.

其中我 select 符合两个条件的 ID,即 ID 3

我对两者都有疑问,但我几乎可以肯定它不正确,因此非常欢迎任何线索。

谢谢

One where I select the ID's that only have status of 'NO' meaning ID 5.

select id from your_table where status='no' and id not in (select id from 
your_table  where status='yes')

One where I select the ID's that match both conditions, meaning ID 3

select id from your_table where status='no' and id  in (select id from 
your_table where status='yes')

最后我认为您期望的 ID 不符合这些条件。所以 UNION 查询并获取你的 table 的 ID,它在 UNION

之后不存在
select id from your_table where id not in (
    select id from your_table where status='no' and id not in 
    (select id from your_table where status='yes')
    union all
    select id from your_table where status='no' and id  in 
    (select id from your_table where status='yes')
)