duanheyi7147 2017-07-20 09:07
浏览 95
已采纳

删除单元格MySQL中的重复值

I have a table with a column 'search_text' type text.

In that field I have values:

 1. 'MyBook MyBook PDF PDF', 
 2. 'Example 1 Example 2 Example 3'
 3. 'John Snow John Snow'

I would like to distinct clean these fields.

Expected result:

 1. 'MyBook PDF', 
 2. 'Example 1 2 3'
 3. 'John Snow'

The approach I came up with goes as follows: read the field for each record, split it by space (' '), put each text in array, do array_unique in PHP, then put the array back to string with join in PHP.

The thing is, this is a PHP based solution, I would like to have an MySQL solution for this. I have over 180.000 records I need to clean, I don't know what impact it would have to run this on PHP.

I have found a solution for MS SQL: Remove duplicate values in a cell SQL Server

Help greatly appreciated.

SQL of my test data:

CREATE TABLE IF NOT EXISTS `test` (
`id` int(10) unsigned NOT NULL,
  `search_text` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `test` (`id`, `search_text`) VALUES
(1, 'MyBook MyBook PDF PDF'),
(2, 'Example 1 Example 2 Example 3'),
(3, 'John Snow John Snow'),
(4, 'test test test test formula test test test formula test test test formula test test test formula test test test formula test test test formula '),
(5, '');

ALTER TABLE `test`
 ADD PRIMARY KEY (`id`);

ALTER TABLE `test`
MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;
  • 写回答

3条回答 默认 最新

  • dqqfuth6736 2017-08-03 12:11
    关注

    I went for the PHP solution here:

    $s = 'John Snow John Snow';
    //remove duplicate values in string
    $tmpArray = explode(" ", $s);
    $tmpArray = array_unique($tmpArray);
    $s = join(" ", $tmpArray);
    

    Which is run before INSERT, and it does what I wanted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答