Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Duplicated Substrings

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

I have a table that has the following structure, the keyword column and title column concat into the 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 of "deduping" the string. So my table will look more like the below:

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

Is this 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 |
+------------+------------+-------------+-----------------------------+
like image 418
BubblewrapBeast Avatar asked Jan 15 '15 13:01

BubblewrapBeast


1 Answers

Try this:

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

Check this SQL FIDDLE DEMO

OUTPUT

|    KEYWORD |      TITLE |           MASHUP |
|------------|------------|------------------|
|      Green |      Green |            Green |
|      Green | Watermelon | Green Watermelon |
| Watermelon | Watermelon |       Watermelon |
like image 97
Saharsh Shah Avatar answered Nov 05 '22 00:11

Saharsh Shah