简体   繁体   中英

MySql query between IN FIND_IN_SET

I have below data in my database table

id          activity          age_criteria
1           act1              18-25,26-39,40-59
2           act2              26-39,40-59
3           act3              40-59,60-79
4           act4              60-79
5           act5              18-25,60-79

Now we have search filter and there is only one text box that can enter one value at a time

like i am searching value "25" than i want 2 records(id 1,5) from above table. need below result.

id          activity          age_criteria
1           act1              18-25,26-39,40-59
5           act5              18-25,60-79

Please help me to resolve this with best way i am thinking with find_in_set and between with same query is it possible?

You can use FIND_IN_SET in combination with REPLACE if set of values are in specific range like 18-25 etc.

Example:

select required_fields
  from activity_age_relation
 where find_in_set( '25', replace( age_criteria, '-', ',' ) ) > 0

Refer to Documentation :

  • FIND_IN_SET(str,strlist)
    • Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

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