[英]Replace Matching Row String From Another Column
I have this table: 我有这张桌子:
ID NewName OldName Link
1 NewName1 OldName1 OldName2|OldName3
2 NewName2 OldName2 OldName1|OldName3
3 NewName3 OldName3 OldName1|OldName2
What I want to happen is to change all the OldName
on the Link
column to the NewName
. 我想要发生的是将“ Link
列上的所有OldName
更改为NewName
。 Like this: 像这样:
ID NewName OldName Link
1 NewName1 OldName1 NewName2|NewName3
2 NewName2 OldName2 NewName1|NewName3
3 NewName3 OldName3 NewName1|NewName2
Can anyone suggest what's the best way to do this? 谁能建议最好的方法是什么?
You are looking to Change the value of Link according OldNames to its with New Names: 您正在寻找将“旧名称”链接的值更改为“新名称”:
First you will need to split your Link
data delimited by |
首先,您将需要分割由|
分隔的Link
数据|
into row & then Join with Your Table 排成一行然后与您的表联接
SELECT TTT.ID,
TTT.[NewName],
TTT.OldName,
[Link] = STUFF(
(
SELECT
'|'+[Link]
FROM
(
SELECT AA.ID,
AA.[NewName],
AA.OldName,
T.[NewName] [Link]
FROM
(
SELECT ID,
NewName,
OldName,
split.x.value('.', 'NVARCHAR(MAX)') DATA
FROM
(
SELECT ID,
NewName,
OldName,
CAST('<M>'+REPLACE(Link, '|', '</M><M>')+'</M>' AS XML) AS String
FROM <table_name>
) AS a
CROSS APPLY String.nodes('/M') AS split(x)
) AA
INNER JOIN <table_name> T ON T.OldName = AA.DATA
) TT
WHERE TT.ID = TTT.ID FOR XML PATH('')
), 1, 1, '')
FROM <table_name> TTT;
Result : 结果:
ID NewName OldName Link
1 NewName1 OldName1 NewName2|NewName3
2 NewName2 OldName2 NewName1|NewName3
3 NewName3 OldName3 NewName1|NewName2
MSSQL: To store delimited-values from other column/other sources to a specific row, you may use FOR XML PATH , look at this SO thread : MSSQL:要将定界值从其他列/其他源存储到特定行,可以使用FOR XML PATH ,请看以下SO线程 :
UPDATE YourTable
SET Link = SUBSTRING(
(
SELECT '|' + T2.NewName
FROM YourTable T2
WHERE '|'+T2.Link+'|' LIKE '%|'+YourTable.OldName+'|%'
FOR XML PATH ('')
), 2, 1000);
MYSQL: If you want to store delimited-values from other column/other sources to a specific row, you may use mysql GROUP_CONCAT function: MYSQL:如果要将定界值从其他列/其他源存储到特定行,则可以使用mysql GROUP_CONCAT函数:
UPDATE table t1
SET Link = (
SELECT GROUP_CONCAT(t2.NewName SEPARATOR '|')
FROM table t2 WHERE FIND_IN_SET(t2.OldName, REPLACE(t1.Link, '|', ','))
)
I assumed that you want to replace any old values in the Link column with its new value. 我假设您想用其新值替换“链接”列中的所有旧值。 See the results in action on dbfiddle.uk 在dbfiddle.uk上查看实际结果
If the Link
column have only two names always, then we try self joining twice to match the respective new names which should be used for replacement. 如果“ Link
列始终只有两个名称,则我们尝试两次自我连接以匹配应用于替换的相应新名称。 The join condition is ugly, but this is the price paid for storing denormalized data in your table. 连接条件很丑陋,但这是为在表中存储非规范化数据而付出的代价。
WITH cte AS
(
SELECT t1.Link, t2.NewName AS NewNameLeft, t3.NewName AS NewNameRight
FROM yourTable t1
LEFT JOIN yourTable t2
ON SUBSTRING(t1.Link, 1, CHARINDEX('|', t1.Link) - 1) = t2.OldName
LEFT JOIN yourTable t3
ON SUBSTRING(t1.Link,
CHARINDEX('|', t1.Link) + 1,
LEN(t1.Link) - CHARINDEX('|', t1.Link)) = t3.OldName
)
UPDATE cte
SET Link = NewNameLeft + '|' + NewNameRight
WHERE NewNameLeft IS NOT NULL AND NewNameRight IS NOT NULL;
Note that this answer assumes that each old name appears only once in the table. 请注意,此答案假定每个旧名称在表中仅出现一次。 I default to not doing an update unless both left and right new names are found. 我默认不进行更新,除非找到左右两个新名称。
I guess you just need REPLACE
我想你只需要REPLACE
select id, NewName, OldName, replace(link, 'OldName', 'NewName') Link
from your_data
and if you need to do it directly in table then use 如果您需要直接在表格中进行操作,请使用
update your_data
set link = replace(link, 'OldName', 'NewName')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.