mySQL:查找 VARCHAR 字段中字符串的重复项?

标签 mysql sql

我有一张这样的 table

TITLE          |   DESCRIPTION
------------------------------------------------
test1          |   value blah blah value
test2          |   value test
test3          |   test test test
test4          |   value test value test

如何仅选择包含后续冗余字符串的行(“blah blah”,而不是“blah bleh blah”)?

所需的输出应该只是

TITLE          |   DESCRIPTION
------------------------------------------------
test1          |   value blah blah value
test3          |   test test test

最佳答案

您可以针对此问题(以及许多其他问题)创建一个包含自然数的辅助表(仅一次)。它可用于多种用途:

create table seq (num int);
insert into seq values (1),(2),(3),(4),(5),(6),(7),(8);
insert into seq select num+8  from seq;
insert into seq select num+16 from seq;
insert into seq select num+32 from seq;
insert into seq select num+64 from seq;
/* continue doubling the number of records until you feel you have enough */

然后您可以在查询中联接该表,其中每个数字用作短语中单词的序列号。这样您就可以提取每个单词并将其与下一个进行比较:

select     title, description
from       phrases
where      description not in (
        select     description
        from       phrases p
        inner join seq 
                on seq.num <= length(p.description)
                            - length(replace(p.description,' ',''))
               and substring_index(substring_index(
                                   description, ' ', num), ' ', -1)
                   = substring_index(substring_index(
                                   description, ' ', num+1), ' ', -1)
        )

示例数据的输出为:

| title |           description |
|-------|-----------------------|
| test2 |            value test |
| test4 | value test value test |

SQL fiddle

关于mySQL:查找 VARCHAR 字段中字符串的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37553151/

相关文章:

sql - 在单个 select 语句中显式输入多行

php - 这些 SQL 语句有什么问题?

mysql - 自加入获取普通学生的教师名单

sql - 如何在 Access VBA 中使用 SQL SELECT 语句

mysql - 在更新中使用子查询转换此选择

mysql - 从 EC2 服务器上安装的 phpMyAdmin 连接到 RDS 实例

mysql - 使用 sum 和 where

c# - SQL 连接

mysql - 如何将多个id分配给MySQL中的一行?

mysql - 从 MySQL 中多个表的 max 列获取关联数据