我有 2 組不同的表,有 2 個公共列。
表一:學生
| 名稱 | ID | 迪普特 | 年 | 批 |
|---|---|---|---|---|
| 美國廣播公司 | 123 | 987 | 2020年 | 2 |
| 莫諾 | 234 | 987 | 2020年 | 2 |
| 美國廣播公司 | 123 | 765 | 2020年 | 2 |
| 齊茲 | 124 | 765 | 2021年 | 1 |
| 美國廣播公司 | 123 | 986 | 2020年 | 2 |
表 2:映射
| ID | 迪普特 |
|---|---|
| 123 | 765 |
| 234 | 987 |
| 123 | 986 |
現在,我試圖找到表 STUDENT 的所有細節,這些細節不存在表 MAPPING 。
例子
| 名稱 | ID | 迪普特 | 年 | 批 | 失蹤 |
|---|---|---|---|---|---|
| 美國廣播公司 | 123 | 987 | 2020年 | 2 | 是的 |
| 齊茲 | 124 | 765 | 2021年 | 1 | 是的 |
我嘗試使用減號,但如果列相同,它會起作用。
任何幫助都會很棒。
問候,
阿拜
uj5u.com熱心網友回復:
看起來很普通NOT EXISTS。采樣資料直到第 13 行;查詢從第 14 行開始。
SQL> with
2 student (name, id, deptid, year, batch) as
3 (select 'abc', 123, 987, 2020, 2 from dual union all
4 select 'mno', 234, 987, 2020, 2 from dual union all
5 select 'abc', 123, 765, 2020, 2 from dual union all
6 select 'zyz', 124, 765, 2021, 1 from dual union all
7 select 'abc', 123, 986, 2020, 2 from dual
8 ),
9 mapping (id, deptid) as
10 (select 123, 765 from dual union all
11 select 234, 987 from dual union all
12 select 123, 986 from dual
13 )
14 select s.name, s.id, s.deptid, s.year, s.batch, 'YES' missing
15 from student s
16 where not exists (select null
17 from mapping m
18 where m.id = s.id
19 and m.deptid = s.deptid
20 )
21 order by s.name;
NAM ID DEPTID YEAR BATCH MIS
--- ---------- ---------- ---------- ---------- ---
abc 123 987 2020 2 YES
zyz 124 765 2021 1 YES
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399483.html
上一篇:Azure托管SQL:將表中的所有欄位顯示為已保存的視圖
下一篇:如何解決嵌套聚合函式錯誤?
