简体   繁体   中英

how to find a comma with mysql find_in_set

is it possible to find a commma with find_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. (So I want to filter to only rows that have a tag that's in the vfilterList). However some tag strings have commas in them. (in vfilterList the commas in tags would be escaped?).

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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