如何 select 一个 table 中的项目在 SQL 中的另一个 table 中不存在
How to select item in one table that is not present in another table in SQL
以下 table 已重组 table
TID ITEMS TIMES TWU
1 D 5 633
1 M 5 665
1 R 14 861
1 F 4 871
1 I 8 910
1 A 7 942
1 N 7 950
1 Z 2 986
1 H 2 1020
2 S 4 551
2 R 7 861
2 F 6 871
2 I 4 910
2 A 6 942
2 N 8 950
2 Z 6 986
2 H 2 1020
3 U 4 354
3 V 7 528
3 B 2 641
3 J 4 842
3 F 4 871
3 I 2 910
3 A 6 942
3 N 2 950
3 Z 4 986
4 X 4 338
4 O 2 442
4 D 2 633
4 B 6 641
4 M 1 665
4 F 5 871
4 A 1 942
4 N 7 950
4 Z 10 986
4 H 1 1020
5 T 5 365
5 C 8 370
5 K 7 397
5 Q 5 397
5 P 5 471
5 S 3 551
5 D 1 633
5 B 6 641
5 M 6 665
5 J 4 842
5 R 6 861
5 I 1 910
5 A 4 942
5 Z 10 986
5 H 7 1020
6 L 5 305
6 U 1 354
6 K 2 397
上面table是升序排列的..我把twu的item的最小值作为每个TID的叶子节点..剩下的item是中间节点..下面的table是LEAFNODES
TID ITEMS
1 D
2 S
3 U
4 X
5 T
6 L
现在我想 select LEAFNODES 中的 ITEMS,在重组
中不作为中间节点存在
要获取另一个 table 中不存在的记录,您可以使用 NOT EXISTS。您想知道中间记录是否存在。您将所有没有 minimun twu per tid 的记录视为中间记录。因此,所有 twu 高于最小值的记录都是中间值,并且您想要 select 来自非中间值的叶节点的所有项目。
select *
from leafnodes
where not exists
(
select *
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
and reorganized.items = leafnodes.items
);
这里有一个 IN 子句,我认为它的可读性更高一些。在这里,您将中间项目视为一个集合,并且您希望一个项目不在该集合中。
select *
from leafnodes
where items not in
(
select items
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
);
select * from LEAFNODES
Excluse
(with temp as
(select TID , min(TWU) as TWU from LEAFNODES )
select TID , Items from REORGANISED as a inner join temp on a.TID=temp.TID and a.TWU=Temp.TWU))
;WITH CTE1 AS
(
-- Select the TId with minimum value from REORGANISED
SELECT TID,MIN(TWU)TWU
FROM REORGANISED
GROUP BY TID
)
,CTE2 AS
(
-- Now we will get value(ITEMS) for the min value and TID from CTE1 for
SELECT C1.TID,R.ITEMS,C1.TWU
FROM CTE1 C1
JOIN REORGANISED R ON C1.TID=R.TID AND C1.TWU=R.TWU
)
SELECT L.*
FROM CTE2 C2
JOIN LEAFNODES L ON C2.TID=L.TID AND C2.ITEMS=L.ITEMS
以下 table 已重组 table
TID ITEMS TIMES TWU
1 D 5 633
1 M 5 665
1 R 14 861
1 F 4 871
1 I 8 910
1 A 7 942
1 N 7 950
1 Z 2 986
1 H 2 1020
2 S 4 551
2 R 7 861
2 F 6 871
2 I 4 910
2 A 6 942
2 N 8 950
2 Z 6 986
2 H 2 1020
3 U 4 354
3 V 7 528
3 B 2 641
3 J 4 842
3 F 4 871
3 I 2 910
3 A 6 942
3 N 2 950
3 Z 4 986
4 X 4 338
4 O 2 442
4 D 2 633
4 B 6 641
4 M 1 665
4 F 5 871
4 A 1 942
4 N 7 950
4 Z 10 986
4 H 1 1020
5 T 5 365
5 C 8 370
5 K 7 397
5 Q 5 397
5 P 5 471
5 S 3 551
5 D 1 633
5 B 6 641
5 M 6 665
5 J 4 842
5 R 6 861
5 I 1 910
5 A 4 942
5 Z 10 986
5 H 7 1020
6 L 5 305
6 U 1 354
6 K 2 397
上面table是升序排列的..我把twu的item的最小值作为每个TID的叶子节点..剩下的item是中间节点..下面的table是LEAFNODES
TID ITEMS
1 D
2 S
3 U
4 X
5 T
6 L
现在我想 select LEAFNODES 中的 ITEMS,在重组
中不作为中间节点存在要获取另一个 table 中不存在的记录,您可以使用 NOT EXISTS。您想知道中间记录是否存在。您将所有没有 minimun twu per tid 的记录视为中间记录。因此,所有 twu 高于最小值的记录都是中间值,并且您想要 select 来自非中间值的叶节点的所有项目。
select *
from leafnodes
where not exists
(
select *
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
and reorganized.items = leafnodes.items
);
这里有一个 IN 子句,我认为它的可读性更高一些。在这里,您将中间项目视为一个集合,并且您希望一个项目不在该集合中。
select *
from leafnodes
where items not in
(
select items
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
);
select * from LEAFNODES
Excluse
(with temp as
(select TID , min(TWU) as TWU from LEAFNODES )
select TID , Items from REORGANISED as a inner join temp on a.TID=temp.TID and a.TWU=Temp.TWU))
;WITH CTE1 AS
(
-- Select the TId with minimum value from REORGANISED
SELECT TID,MIN(TWU)TWU
FROM REORGANISED
GROUP BY TID
)
,CTE2 AS
(
-- Now we will get value(ITEMS) for the min value and TID from CTE1 for
SELECT C1.TID,R.ITEMS,C1.TWU
FROM CTE1 C1
JOIN REORGANISED R ON C1.TID=R.TID AND C1.TWU=R.TWU
)
SELECT L.*
FROM CTE2 C2
JOIN LEAFNODES L ON C2.TID=L.TID AND C2.ITEMS=L.ITEMS