简体   繁体   中英

MySQL Search query with like

i have database table with few text fields which i use to filter data, each of this text fields contain number data separated with colon ( for example '1:2:4:5:6:7:9' )

id   |  title         |  field1                 | filed2     | field3            |
1    | Some title1    |1:2:3:4:5:6:7            |1:2:3:6:7   | 1:2:4:5:7:9:10:11 | 
2    | Some title2    |1:2:3:4:5:6:7:8:9:10:11  |1:2:3:4:5:6 | 1:2:4:5:7:9:10:11 |
3    | Some title3    |1:2:3:4:5:6:7            |1:2:3:6:9   | 1:2:4:5:7:9:10:11 |
4    | Some title4    |3:4:5:6:7:10:11          |1:2:3:10:11 | 1:2:4:5:7:9:10:11 |
5    | Some title5    |1:2:3:4:5:6:7            |1:2:3:6:7   | 1:2:4:5:7:8:9:11  |
6    | Some title6    |2:3:4:5:6:7:9            |1:2:3:6:7   | 1:2:4:5:7:9       |

using a search form i use check boxes for each parameters for field1, field2 and field3 and use php foreach to create LIKE '% parameter %' for each field

my SQL query look like SELECT * FROM table WHERE (field1 LIKE '%1%' OR field LIKE '%2%' ) AND (field2 LIKE '%1%' OR field2 LIKE '%2%')

unfortunately this query doesn't give me good result since i get back rows which doesn't match my idea:)

for example if checkboxes values 1,2,3,4,5,6,7 are selected by user query need to return only 1, 3 and 5th row

is there any better solution for filtering this results

regards

Additional Info:

Let me try to explain you a little bit more the idea
User check chekboxes with some values (ie. 1,3,4,5,6,7) script need to return only results which contain those values in field1 same thing happens when user check other checkboxes which control field2 and field3 but for now it's not important

Additional info pt2.

I'll try to give you all better explanation what i want to achieve

I have search form with checkboxes and radio input fields, each checkbox group ( values from 1 to 10 ) are represent columns ( field1, field2, field3 ) in table, each value in columns field1 for example represent selected checkboxes ( which are set in admin panel for each row ) now by default all check boxes are selected and i get all results now when user deselect respective checkbox results need to be filtered up with rows according to filter good example what i try to achieve is http://www.hotelscombined.com/City/London.htm

so if checkbox with value 2 is deselected it can't be listed in results

BR Sile

I think the problem is that %1% matches not only 1 but also 10 , 11 , etc.

It's better to do something like this:

WHERE CONCAT(':', field1, ':') LIKE '%:1:%'

As you see, field2 LIKE '%1%' will match field with value 2:10. To prevent this, add colon to end and beginning of string, and search for LIKE '%:1:%' Other possible solution- adding colons to end and beginning in SQL CONCAT(':',field2,':') LIKE '%:1:%'

Other way is to refactor your database to something like

main_table
id   |title
1    |some title

link_table
main_id|field|value
1      |f1   |1     
1      |f1   |2     
1      |f2   |2     
1      |f3   |1     
1      |f3   |2    

and to make query to get data

select id,title from main_table
join link_table on main_table.id=link_table.main_id
where (field = 'f1' and value in (1,2)) or (field = 'f2' and value in (2,3))

as result, if you're using your current database design, if you need to get all rows, that have set of "1,2,3,4,5,6,7" in field 1, you'll need to make a query like

WHERE CONCAT(':',field1,':') LIKE '%:1:%' OR  
CONCAT(':',field1,':') LIKE '%:2:%' OR 
CONCAT(':',field1,':') LIKE '%:3:%' OR
CONCAT(':',field1,':') LIKE '%:4:%' OR
CONCAT(':',field1,':') LIKE '%:5:%' OR
CONCAT(':',field1,':') LIKE '%:6:%' OR
CONCAT(':',field1,':') LIKE '%:7:%'

If you'll refactor as I say, query will be like

where field = 'f1' and value in (1,2,3,4,5,6,7)

and much faster

Use AND in instead of OR:

SELECT * FROM table WHERE (field1 LIKE '%1%' AND field LIKE '%2%' ) AND (field2 LIKE '%1%' AND field2 LIKE '%2%')

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