AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 303813
Accepted
Nuno
Nuno
Asked: 2021-12-11 16:32:03 +0800 CST2021-12-11 16:32:03 +0800 CST 2021-12-11 16:32:03 +0800 CST

过滤掉当前行的文本包含在下一行的文本中的行

  • 772

我想知道是否可以在 aSELECT中过滤掉当前行上的文本包含在下一行的文本中的行。

例如,我们记录用户执行的“搜索”的图像,“当他们键入时”:

1  something I searched
2  another search
3  this
4  this is a
5  this is a third
6  this is a third search
7  fourth
8  fourth search
9  here's a ry
10 here's a typo

第3-6 行实际上是相同的——只是用户打字有点“慢”,所以我们保存了多次。
第 7-8 行也一样。
第9-10 行是相同的,但有一个错字。

我正在寻找SELECT仅返回以下行的操作:

1  something I searched
2  another search
6  this is a third search
8  fourth search
9  here's a ry
10 here's a typo

这样的事情可能吗?

注意 - 完全不能保证 ID 将按特定用户的顺序排列。所以,我不能简单地JOIN用表本身,由id = id+1. 我能做的就是ORDER BY id把它们整理好。

mysql mariadb
  • 1 1 个回答
  • 56 Views

1 个回答

  • Voted
  1. Best Answer
    J.D.
    2021-12-12T06:02:59+08:002021-12-12T06:02:59+08:00

    你会想要使用一个窗口函数,特别是这个LEAD()函数会在这里很好地使用,如下所示:

    -- Gets the following row's search text and compare it to the current row's search text to see if the following row starts with the current row's
    WITH _SearchesMarkedAsRedundant
    (
        SELECT 
            id, 
            userId,
            searchText,             
            IF(LEAD(searchText) OVER (PARTITION BY userId ORDER BY id) LIKE CONCAT(searchText, '%'), 1, 0) ​IsRedundantSearch
        FROM Searches
    )
    
    -- Returns only the rows whose following row did not start with this row's search text
    SELECT id, userId, searchText
    FROM _SearchesMarkedAsRedundant
    WHERE IsRedundantSearch = 0
    

    如果你想全局删除冗余搜索,不管是哪个用户输入了搜索,你可以PARTITION BY像这样删除窗口函数的子句IF(LEAD(searchText) OVER (userId ORDER BY id) LIKE CONCAT(searchText, '%'), 1, 0) ​IsRedundantSearch。

    以上将完成您给定示例的输出。请注意,它仅查看下1 行(按 id 列排序)并检查该行是否以与当前行相同的搜索文本开头。您可以通过在运算符的前面添加另一个通配符来使其执行完整的包含检查,如下所示。%searchTextCONCAT()CONCAT('%', searchText, '%')

    窗口函数是这里解决方案的LEAD()关键,因为它可以在您指定的任意多行输入的列中进行前瞻。它预读的默认行数为 1,但如果您想查看前 3 行而不是下一行,则预读LEAD(searchText, 3)3 行。

    • 2

相关问题

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve