简体   繁体   中英

SQL & FIND_IN_SET for filtering

On the main page I have a list of establishments, which you can filter with location, name and services provided by that establishment. I have SQL table, which has list of establishments with numeric definition of services provided:

id | name            | location | services
1  | Establishment 1 | Florida  | 2,5
2  | Establishment 2 | New York | 1,3,5

On the main page services are supposed to be filtered by checking checkboxes.

I'm looking for SQL query which will filter results based on selected services (eg if user selected service 5 - it will show both results from the table above, but if user selects services 5 and 1 - only service with id 2, if selects 5 and 4 - no results).

I found out that FIND_IN_SET might work, but it seems that it doesn't work if user selects more than one service:

SELECT * FROM table1 t WHERE FIND_IN_SET(?, t.services) > 0

? - bind input, which is sent by POST AJAX as concatenation of selected services divided by comas - eg. services = "1,3,4".

You can try REGEXP :

Check this:

SELECT * FROM testtbl t WHERE t.services REGEXP '(^|,)(1|3|4)(,|$)'

You need to pass your comma separated value in this form => 1|3|4

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