Removing duplicate substrings

I understand that the following question may not be best practice.

I have a table that has the following structure: keyword column and concat column heading to mashup column.

  + ------------ + ------------ + ----------------------- +
 |  Keyword |  Title |  Mashup |
 + ------------ + ------------ + ----------------------- +
 |  Green |  Green |  Green Green |
 |  Green |  Watermelon |  Green Watermelon |
 |  Watermelon |  Watermelon |  Watermelon Watermelon |
 + ------------ + ------------ + ----------------------- +

I would like to know if there is a way to "deduplicate" a string. So my table will look something like this:

  + ------------ + ------------ + ----------------------- +
 |  Keyword |  Title |  Mashup |
 + ------------ + ------------ + ----------------------- +
 |  Green |  Green |  Green |
 |  Green |  Watermelon |  Green Watermelon |
 |  Watermelon |  Watermelon |  Watermelon |
 + ------------ + ------------ + ----------------------- +

Is it possible? I can't seem to find a solution. Thanks!

EDIT:

  + ------------ + ------------ + ------------- + --------- -------------------- +
 |  Keyword |  Title |  Another |  Mashup |
 + ------------ + ------------ + ------------- + --------- -------------------- +
 |  Green |  Green |  Pink |  Green Green Pink |
 |  Green |  Watermelon |  Yellow |  Green Watermelon Yellow |
 |  Watermelon |  Watermelon |  Black |  Watermelon Watermelon Black |
 + ------------ + ------------ + ------------- + --------- -------------------- +
+5
source share
1 answer

Try the following:

UPDATE tableA SET Mashup = IF(Keyword = Title, Keyword, CONCAT(Keyword, ' ', Title)); 

Mark SQL FIDDLE DEMO

OUTPUT

 | KEYWORD | TITLE | MASHUP | |------------|------------|------------------| | Green | Green | Green | | Green | Watermelon | Green Watermelon | | Watermelon | Watermelon | Watermelon | 
+6
source

Source: https://habr.com/ru/post/1211161/


All Articles