删除重复的子串

时间:2015-01-15 13:20:41

标签: mysql sql if-statement sql-update concat

我理解以下问题可能不是最佳做法。

我有一个具有以下结构的表,关键字列和标题列连接到mashup列。

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

我想知道是否有一种方法可以重复使用"字符串。所以我的表格看起来更像下面的内容:

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

这可能吗?我似乎无法找到解决方案。谢谢!

编辑:

+------------+------------+-------------+-----------------------------+
| Keyword    | Title      | Another     | Mashup                      |
+------------+------------+-------------+-----------------------------+
| Green      | Green      | Pink        | Green Green Pink            |
| Green      | Watermelon | Yellow      | Green Watermelon Yellow     |
| Watermelon | Watermelon | Black       | Watermelon Watermelon Black |
+------------+------------+-------------+-----------------------------+

1 个答案:

答案 0 :(得分:6)

试试这个:

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

选中此SQL FIDDLE DEMO

<强>输出

|    KEYWORD |      TITLE |           MASHUP |
|------------|------------|------------------|
|      Green |      Green |            Green |
|      Green | Watermelon | Green Watermelon |
| Watermelon | Watermelon |       Watermelon |