只有 SELECT 行的日期小于另一个 table 的日期

Only SELECT rows where the date is less than a date from another table

我正在重构一些 SQL 以删除存在于多个 OR 条件中的硬编码日期,作为 INSERT INTO 语句的一部分。

例如:

OR ( site = 'site.com' AND url = 'www.someurl.com' AND date < '2021-01-01')
OR ( site = 'site.com' AND url = 'www.otherurl.com' AND date < '2019-09-09')

站点 = 'site.com' 始终保持不变。

url = 'www.someurl.com' 会有不同的 URL

日期也会改变。

为了删除像上面那样存在的几行,我创建了一个引用 table,其中包含一个站点名称列表,一个用于标记有趣内容的布尔列 url s 和日期(与硬编码相同)

我已经整理好了除 AND date < '2021-01-01' 之外的所有内容。我如何 Table A 中的 select 行,其中 url 与我的引用 table 中的 url 匹配(Table B),其中日期小于引用中的日期 table?

我遇到了以下子查询问题:

OR (site = 'site.com' AND url IN ( SELECT url_ref
                                       FROM REF_TABLE
                                       LEFT JOIN TABLE A
                                       ON url = url_ref) AND date < ( SELECT start_date  
                                                                         FROM REF_TABLE
                                                                         LEFT JOIN TABLE A
                                                                         ON url = url_ref ) )

我能够消除另一个包含 url 列表但没有日期的大型 OR 语句。我被困在这少数情况下,我只需要获取日期小于特定日期的行,如参考文献 table.

中所定义

它是这样工作的:

            OR (ref.intersting = 't' AND url NOT IN ( SELECT url_ref
                                                         FROM REF_TABLE
                                                         WHERE url_ref IS NOT NULL ) )

谢谢!

p.s。这样做的目的是让我可以在每次创建新异常时停止编辑 python 脚本。相反,我将只向引用添加一行 table 并且脚本不需要更改。

您可以使用 EXISTS:

OR
(outer.site = 'site.com' AND 
 EXISTS (SELECT 1
         FROM REF_TABLE r
         WHERE outer.url = r.url AND
               outer.date < r.date
        )
) 

outer 是外部查询中列的 table 别名。