使用 table 2 信息过滤来自 table 1 的行
Filter rows from table 1 with table 2 info
我有 2 个 table。一个 table 列出 运行 的结果,第二个 table 带有时间戳。
table1(不确定如何向 table 添加另一列,但 table 1 也有结束时间,需要过滤掉):
Tenant
start
x
2022-05-01 23:00:00
x
2022-05-02 02:00:00
x
2022-05-02 06:00:00
table 2:
start
end
2022-05-01 23:00:00
2022-05-02 03:00:00
目标是过滤掉/排除 table 1 中介于 table 2.start/end 时间之间的行。
结果应该是:
Tenant
start
x
2022-05-02 06:00:00
我试过以下操作:
select *
from table 1
where start not in (select start from table 2)
但似乎无法正常工作。
我们可以尝试使用现有逻辑:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.start BETWEEN t2.start AND t2.end
);
对于您更新后的要求,我们可以select一个存在的布尔表达式:
SELECT t1.*, NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.start BETWEEN t2.start AND t2.end
) AS mw
FROM table1 t1;
我有 2 个 table。一个 table 列出 运行 的结果,第二个 table 带有时间戳。
table1(不确定如何向 table 添加另一列,但 table 1 也有结束时间,需要过滤掉):
Tenant | start |
---|---|
x | 2022-05-01 23:00:00 |
x | 2022-05-02 02:00:00 |
x | 2022-05-02 06:00:00 |
table 2:
start | end |
---|---|
2022-05-01 23:00:00 | 2022-05-02 03:00:00 |
目标是过滤掉/排除 table 1 中介于 table 2.start/end 时间之间的行。
结果应该是:
Tenant | start |
---|---|
x | 2022-05-02 06:00:00 |
我试过以下操作:
select *
from table 1
where start not in (select start from table 2)
但似乎无法正常工作。
我们可以尝试使用现有逻辑:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.start BETWEEN t2.start AND t2.end
);
对于您更新后的要求,我们可以select一个存在的布尔表达式:
SELECT t1.*, NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.start BETWEEN t2.start AND t2.end
) AS mw
FROM table1 t1;