简体   繁体   English

如何用mysql查找逗号find_in_set

[英]how to find a comma with mysql find_in_set

is it possible to find a commma with find_in_set ? 是有可能找到一个commmafind_in_set

I tried: 我试过了:

select find_in_set('\,', '\,');

my use case is: 我的用例是:

where find_in_set(r.tag, vfilterList)

here, vfilterList is a comma separated list provided as input to a stored procedure - and r.tag is the tag string in the table. 在这里,vfilterList是一个逗号分隔的列表,作为存储过程的输入提供-r.tag是表中的标记字符串。 (So I want to filter to only rows that have a tag that's in the vfilterList). (所以我只想过滤到vfilterList中具有标签的行)。 However some tag strings have commas in them. 但是,某些标记字符串中包含逗号。 (in vfilterList the commas in tags would be escaped?). (在vfilterList中,标记中的逗号是否会转义?)。

Something tells me i'm doing it wrong? 告诉我我做错了吗?

No, it isn't possible. 不,不可能。

The documentation specifically points out that this doesn't work. 该文档特别指出这是行不通的。

This function does not work properly if the first argument contains a comma (“,”) character. 如果第一个参数包含逗号(“,”)字符,则此功能无法正常工作。

http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

The function is designed to return the ordinal position of a substring, delimited by commas. 该函数旨在返回以逗号分隔的子字符串的顺序位置。 By definition, essentially, it would never find anything with a comma in it, since it's only considering the values between them. 根据定义,从本质上讲,它永远不会找到带有逗号的任何内容,因为它仅考虑它们之间的值。

Depending on what you're actually trying to accomplish, INSTR() or SUBSTRING_INDEX() might be useful. 根据您实际要完成的工作, INSTR()SUBSTRING_INDEX()可能会有用。

Ended up just replacing commas with pipes. 最终只是用管道替换了逗号。 (and doing the same for each tag within the filter list). (并对过滤器列表中的每个标签执行相同的操作)。 It's reasonably accurate with false positives very unlikely for our use case. 对于我们的用例来说,它具有相当准确的误报率是非常不可能的。

where find_in_set(replace(r.tag,",","|"), vfilterList)

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

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