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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)