Removing Duplicated Substrings

不问归期 提交于 2019-12-12 11:27:06

问题


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 |
+------------+------------+-------------+-----------------------------+

回答1:


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 |


来源:https://stackoverflow.com/questions/27964397/removing-duplicated-substrings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!