使用来自一个 table 的过滤条件来动态地 select 来自另一个 table 的数据 联合所有结果

Use filter criteria from one table to dynamically select data from another table an union all results

是否可以使用 sqlite 创建这样一个循环查询 table 一次选择一行,我可以将其用作另一个(复杂)查询及其子查询的参数?这是我的意思的简短演示。

有一个 table 符合条件:

create temp table filter as
    select 'foo' name, '+1 day' offset union
    select 'bar' name, '+3 day' offset union
    select 'baz' name, '+6 day' offset;    

这是 table 的数据:

create temp table data as
    select 'foo' name, '2021-12-05' day union
    select 'bar' name, '2021-12-06' day union
    select 'foo' name, '2021-12-07' day union
    select 'baz' name, '2021-12-08' day union
    select 'baz' name, '2021-12-09' day union
    select 'bar' name, '2021-12-10' day;

现在,我想遍历筛选行并使用每一行查询第二个 table 的数据,然后 union 所有结果。显然下面的伪代码不起作用:

foreach filter
   previous result
   union all
   select * 
   from data
   where date(day) = date('2021-12-07', filter.offset)

我正在考虑递归 CTE,但我不确定在涉及另一个 table 时如何将它组合在一起。

您描述的是 2 个表的连接:

SELECT d.* 
FROM data d INNER JOIN filter f
ON d.name = f.name AND d.day = date('2021-12-07', f.offset);

我在 ON 子句中添加了条件 d.name = f.name 因为 2 列的相似性可能相关。
如果实际上不需要,请删除条件。

参见demo