-2

我有一张桌子callInfo,它看起来像这样:

+----+------------------------------------------------------------------------------------------------------------------------------------+
| id | idUrl                                                                                                               | collectionId |                                                                                              |
+----+------------------------------------------------------------------------------------------------------------------------------------+
|  1 | id?books.0.levelOfDetail=high&books.0.shopId=727&books.0.type=books&collectionId=20092014&type=seasonPassSource     |          |
|  2 | id:call3?books.0.levelOfDetail=high&books.0.shopId=123&books.0.type=books&collectionId=16645&type=seasonPassSource  |          |
|  3 | id:call3?maxDepth=1&parentMixId=777&type=mixSource                                                                  |          |
|  4 | idSet:call3?keyword=%22FOO%20BAR%20.%5E%24*%2B%3F%7C%28%29%7B%7D%5B%5D%22&type=wishListSource                       |          |
|  5 | idSet:call3?books.0.levelOfDetail=high&books.0.shopId=727                                                           |          |
|  6 | idSetSource.0.booksNumber=2&collectionId=16645&books.0.levelOfDetail=high&books.0.type=books&type=seasonPassSource  |          |
|  7 | idSet:call3?keyword=hero&type=wishListSource                                                                        |          |
+----+-------------------------+----------------------------------------------------------------------------------------------------------+

我在表中创建了一个名为的新列collectionIdcallInfo但表中有许多记录需要更新此列值。

我需要collectionIdidUrl列中提取值并将其放入collectionId列中。

它应该看起来像这样

+----+------------------------------------------------------------------------------------------------------------------------------------+
| id | idUrl                                                                                                               | collectionId |                                                                                              |
+----+------------------------------------------------------------------------------------------------------------------------------------+
|  1 | id?books.0.levelOfDetail=high&books.0.shopId=727&books.0.type=books&collectionId=20092014&type=seasonPassSource     | 20092014     |
|  2 | id:call3?books.0.levelOfDetail=high&books.0.shopId=123&books.0.type=books&collectionId=16645&type=seasonPassSource  | 16645        |
|  3 | id:call3?maxDepth=1&parentMixId=777&type=mixSource                                                                  | NULL         |
|  4 | idSet:call3?keyword=%22FOO%20BAR%20.%5E%24*%2B%3F%7C%28%29%7B%7D%5B%5D%22&type=wishListSource                       | NULL         |
|  5 | idSet:call3?books.0.levelOfDetail=high&books.0.shopId=727                                                           | NULL         |
|  6 | idSetSource.0.booksNumber=2&collectionId=16645&books.0.levelOfDetail=high&books.0.type=books&type=seasonPassSource  | 16645        |
|  7 | idSet:call3?keyword=hero&type=wishListSource                                                                        | NULL         |
+----+-------------------------+----------------------------------------------------------------------------------------------------------+

我需要在 MySQL 中执行此操作。有任何想法吗?

4

1 回答 1

2

另一种使用方法SUBSTRING_INDEX()

UPDATE callInfo
   SET collectionId = SUBSTRING_INDEX(SUBSTR(idUrl, INSTR(idUrl, 'collectionId=') + 13), '&', 1)
 WHERE idUrl LIKE '%collectionId=%'

输出:

| 身份证 | ... | 收藏ID |
|----| ... |--------------|
| 1 | ... | 20092014 |
| 2 | ... | 16645 |
| 3 | ... | (空) |
| 4 | ... | (空) |
| 5 | ... | (空) |
| 6 | ... | 16645 |
| 7 | ... | (空) |

这是SQLFiddle演示

于 2013-08-28T03:29:04.810 回答