我對如何在 SQL 中執行此操作有些空白。
考慮R中的這個reprex
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)))
給出了這張表
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 查詢。
我選擇只有“NO”狀態的 ID 表示 ID 5。
和
一個我選擇符合這兩個條件的 ID,即 ID 3
我對兩者都有疑問,但我幾乎可以肯定它不正確,所以任何線索都非常受歡迎。
謝謝
uj5u.com熱心網友回復:
我選擇只有“NO”狀態的 ID 表示 ID 5。
select id from your_table where status='no' and id not in (select id from
your_table where status='yes')
一個我選擇符合這兩個條件的 ID,即 ID 3
select id from your_table where status='no' and id in (select id from
your_table where status='yes')
最后,我認為您期望的 id 不符合這些條件。所以UNION查詢和獲取你的表的 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')
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413596.html
標籤:
上一篇:根據Northwind資料庫的銷售額,排名前5位的供應商
下一篇:檢查SQL中的多個可互換列值
