SQL - 根据对另一列的查询在一列中查找和替换

时间:2022-01-06 20:16:55

I don't do too much SQL so need help with something simple.

我没有做太多SQL,所以需要简单的帮助。

I have the following Table:

我有以下表格:

ID term value
--------------
1 term1 slug/
2 term1 slug/
3 term2 something/something
4 term2 slug/
5 term3 slash-/-something

What I want to do is to FIND / in column valueand REPLACE with an empty string BUT only where term is equal to term1

我想要做的是查找/列值和使用空字符串REPLACE但仅限于term等于term1

The result would be:

结果将是:

ID term value
--------------
1 term1 slug
2 term1 slug
3 term2 something/something
4 term2 slug/
5 term3 slash-/-something

I can SELECT everything in term that is equal to term1, and I can FIND AND REPLACE everything in value with a /, but I can't seem to figure out how to combine the two operations together.

我可以在术语中选择等于term1的所有内容,并且我可以使用/来查找和替换值中的所有内容,但我似乎无法弄清楚如何将这两个操作组合在一起。

Note: I recognize there's no FIND AND REPLACE operation.

注意:我认识到没有FIND AND REPLACE操作。

2 个解决方案

#1


1  

You just need to execute an Update command in the database:

您只需在数据库中执行Update命令:

update yourTableNameHere 
   set value = replace(value, '/','') 
 where term = 'term1'

#2


0  

Try this :

尝试这个 :

SELECT ID, term, CASE  WHEN term = 'term1' THEN REPLACE(value, '/','')
        ELSE value
    END AS value
FROM tablename

#1


1  

You just need to execute an Update command in the database:

您只需在数据库中执行Update命令:

update yourTableNameHere 
   set value = replace(value, '/','') 
 where term = 'term1'

#2


0  

Try this :

尝试这个 :

SELECT ID, term, CASE  WHEN term = 'term1' THEN REPLACE(value, '/','')
        ELSE value
    END AS value
FROM tablename