简体   繁体   English

在 MySQL 查询中将所有反斜杠删除为字符串

[英]Remove All Backslash into string in MySQL query

I have this type of string我有这种类型的字符串

'160f7a4a-766a-4c23-a155-8bd3f7389f77\', \'63233bfc-b663-4c73-890b-00a48d79c4dc'

In one column and I want like在一栏中,我想要

'160f7a4a-766a-4c23-a155-8bd3f7389f77','63233bfc-b663-4c73-890b-00a48d79c4dc'

This type of result in MySQL这种类型的结果在 MySQL

i have to perform query like我必须执行类似的查询

SELECT * FROM kapp_staging.kols where `kol_id` in (select REPLACE(json_id,'\'',"'") FROM kapp_staging.news_items 
 where `id` = '991'))

in where in clause i have subquery and in subquery i geting在 where in 子句中我有子查询,在子查询中我得到

'160f7a4a-766a-4c23-a155-8bd3f7389f77\', \'63233bfc-b663-4c73-890b-00a48d79c4dc'

this type of value so i need to remove \ from value so my where in query work fine.这种类型的值所以我需要从值中删除 \ 这样我的查询位置就可以正常工作。

i have data like:我有这样的数据:

Kols table
| id | kol_id                                   | name    | data  |
|----|----------------------------------------  |---------| ------|
| 1  |160f7a4a-766a-4c23-a155-8bd3f7389f77      | balwant | data  |
| 2  |63233bfc-b663-4c73-890b-00a48d79c4dc      | vikram  | data  |

news items
| id | json_id | data    |
|----|-----------------------------------------------------------------------------------------|---------|
| 991  | {'\160f7a4a-766a-4c23-a155-8bd3f7389f77\','\160f7a4a-766a-4c23-a155-8bd3f7389f77\'} | data    |

I tried many ways but didn't get this response.我尝试了很多方法,但没有得到这个回应。

Thanks in Advance: )提前致谢: )

The backslashes aren't in the data, they're just used to escape the quotes when inserting into the table.反斜杠不在数据中,它们只是在插入表时用于转义引号。 So you don't need to remove them.所以你不需要删除它们。

However, you can't use IN to match values in a comma-delimited list, you need to use FIND_IN_SET() ;但是,您不能使用IN来匹配逗号分隔列表中的值,您需要使用FIND_IN_SET() see Search with comma-separated value mysql请参阅使用逗号分隔值搜索 mysql

You also need to remove the quotes and curly braces before you can use FIND_IN_SET() .在使用FIND_IN_SET()之前,您还需要删除引号和花括号。

SELECT DISTINCT k.*
FROM kols AS k
JOIN news_items AS n 
    ON FIND_IN_SET(k.kol_id, 
        REPLACE(REPLACE(REPLACE(json_id, '{', ''), '}', ''), "'", ''))

DEMO演示

Things would be much easier if you normalized your data and put the list of IDs into a separate table with one row per ID.如果您规范化数据并将 ID 列表放入单独的表中,每个 ID 一行,事情会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM