簡體   English   中英

如何使用另一個表中的匹配值替換/更新列中每個字符串的所有實例?

[英]How do I replace/update all instances of every string in a column with matching values from another table?

在逗號分隔的字符串中存儲數據不是由我決定的,這不是我可以在我的數據庫中更改的內容所以請耐心等待。 我已經在網上和stackoverflow上進行了相當多的搜索但是我找不到解決方案,如果它甚至可以使用MySQL。

我試圖用table2中的匹配值替換table1中每個唯一字符串的所有實例。 我嘗試過通配符,替換,更新,加入等等,我只是不確定如何使它工作。 我知道每個字符串的一個解決方案是replace(),但table2有超過200行,這意味着嵌套超過200次。

這就是我想要完成的事情。 我有兩個表,table1:

+------+-------------+
| Item | Code        |
+------+-------------+
| 1    | 614         |
+------+-------------+
| 2    | 212,614,415 |
+------+-------------+
| 3    | 212,303     |
+------+-------------+
| ...  | ...         |
+------+-------------+

和table2:

+------+-------------------+
| Code | Name              |
+------+-------------------+
| 614  | Columbus, OH      |
+------+-------------------+
| 212  | New York, NY      |
+------+-------------------+
| 415  | San Francisco, CA |
+------+-------------------+
| 303  | Ft. Worth, TX     |
+------+-------------------+
| ...  | ...               |
+------+-------------------+

我想用table2中的相應值替換table1中的代碼以生成此結果:

+------+---------------------------------------------+
| Item | Code                                        |
+------+---------------------------------------------+
| 1    | Columbus, OH                                |
+------+---------------------------------------------+
| 2    | New York, NY,Columbus, OH,San Francisco, CA |
+------+---------------------------------------------+
| 3    | New York, NY,Ft. Worth, TX                  |
+------+---------------------------------------------+
| ...  | ...                                         |
+------+---------------------------------------------+

這應該這樣做(參見下面的最后一個查詢)。 我在連接中包含了逗號,因此像12這樣的id與你所擁有的id和212的id不匹配(例如)。

drop table if exists table1;

drop table if exists table2;

create table table1(
    item int,
    code varchar(64)
);

create table table2(
    code int,
    name varchar(64)
);

insert into table1 values (1, '614');
insert into table1 values (2, '212,614,415');
insert into table1 values (3, '212,303');

insert into table2 values(212, 'New York, NY');
insert into table2 values(303, 'Ft. Worth, TX');
insert into table2 values(415, 'San Francisco, CA');
insert into table2 values(614, 'Columbus, OH');

select * from table1

+ --------- + --------- +
| item      | code      |
+ --------- + --------- +
| 1         | 614       |
| 2         | 212,614,415 |
| 3         | 212,303   |
+ --------- + --------- +
3 rows

select * from table2

+ --------- + --------- +
| code      | name      |
+ --------- + --------- +
| 212       | New York, NY |
| 303       | Ft. Worth, TX |
| 415       | San Francisco, CA |
| 614       | Columbus, OH |
+ --------- + --------- +
4 rows

select 
    t1.item,
    t2.name
from
    table1 t1 join table2 t2 on (
        t1.code = t2.code
        or t1.code like concat(t2.code, ',%')
        or t1.code like concat('%,', t2.code, ',%')
        or t1.code like concat('%,', t2.code)
    )
order by t1.item

+ --------- + --------- +
| item      | name      |
+ --------- + --------- +
| 1         | Columbus, OH |
| 2         | Columbus, OH |
| 2         | New York, NY |
| 2         | San Francisco, CA |
| 3         | Ft. Worth, TX |
| 3         | New York, NY |
+ --------- + --------- +
6 rows

編輯:或者如果你想保持數據非規范化,如下所示:

select 
    t1.item,
    group_concat(t2.name)
from
    table1 t1 join table2 t2 on (
        t1.code = t2.code
        or t1.code like concat(t2.code, ',%')
        or t1.code like concat('%,', t2.code, ',%')
        or t1.code like concat('%,', t2.code)
    )
group by t1.item
order by t1.item

+ --------- + -------------------------- +
| item      | group_concat(t2.name)      |
+ --------- + -------------------------- +
| 1         | Columbus, OH               |
| 2         | Columbus, OH,New York, NY,San Francisco, CA |
| 3         | Ft. Worth, TX,New York, NY |
+ --------- + -------------------------- +
3 rows

在這里,我們看到一個完美的例子,說明為什么在數據庫字段中使用逗號分隔列表是一個壞主意。 它們比合適的關系表更難操縱。

考慮到這一點,我會考慮首先將代碼拆分為多個記錄,然后執行基於簡單集的替換,然后將它們重新組合在一起。 實質上:

  1. 使用split函數創建臨時表tmp1,每個項目/代碼對包含1條記錄。

  2. 然后從tmp1加入到table1的tmp1.code上執行UPDATE。

  3. 最后使用GROUP_CONCAT將名稱重新組合在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM