SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
Feb 11, 2005, 18:56 #1
- Join Date
- Jul 2003
- Location
- USA
- Posts
- 11
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Searching w/ Comma Delimited Field
I'm trying to search a table based on categories. I currently have categories stored in a mediumtext field with their ids separated by commas.
Ex. categories = '01,02,03'
I have a search box that allows a person to select the categories to filter by (using checkboxes). Once those categories are selected, I need to run a query to see which users have at least one of those categories in their categories field.
The issue I have is that I need to do this within the query and the categories field contains multiple values instead of just one. For each category that was checked in the form, I need to see if that id exists within the categories field.
I tried placing the id first and then comparing it to the field:
Ex. '$id' IN (categories)
That didn't seem to work although it didn't give any MySQL errors. I thought that maybe it didn't recognize that categories was actually the field vs a string.
Is there a special function I could use to accomplish what I'm looking to do?
Mark
-
Feb 11, 2005, 20:42 #2
- Join Date
- Feb 2005
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
if all the categories ids are 2 digits (00-99) this could work : categories LIKE '%$id%'
--
Mario
-
Feb 11, 2005, 21:37 #3
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
your basic problem is the design of the table
any time you have a comma-delimited list inside a column, you're going to end up with inefficient sql that won't scale
if someone chooses categories 4, 9, and 37 from the checkboxes, you have to actually do three queries (inside a loop)
and each one will do a table scan
i advise you to redesign into a proper many-to-many relationship
then you can use table indexes, and your queries will not only be incredibly simple, but extremely efficient
-
Feb 14, 2005, 03:40 #4
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
evolutionm
The function you're looking for is FIND_IN_SET(). I’d better listen to Rudy's advice though.
Bookmarks