Jump to content

Recommended Posts

say I have a list of categories in a field, and a page that is supposed to pull from the db based on a category. Is there any way I can do a query where it searches the contents of that field for the category in question?

 

like SELECT FROM table WHERE category = $category  but have it search the category field (the comma separated list for each entry) for the $category var

 

 

Link to comment
https://forums.phpfreaks.com/topic/63178-comma-separated-list-in-mysql-field/
Share on other sites

The answer lies in correctly normalising your data and not holding data as comma-separated lists

 

[pre]

instead of

 

+------+---------+------------------+

|  id  |  name  |  categories      |

+------+---------+------------------+

|  1  |  widget |  12, 20, 15      |

|  2  |  gizmo  |  12, 14, 16      |

+------+---------+------------------+

 

you should have

 

+------+---------+          +------+---------+

|  id  |  name  |          |  id  |  cat  |

+------+---------+          +------+---------+

|  1  |  widget |          |  1  |    12  |

|  2  |  gizmo  |          |  1  |    20  |

+------+---------+          |  1  |    15  |

                            |  2  |    12  |

                            |  2  |    14  |

                            |  2  |    16  |

                            +------+---------+

[/pre]

 

then it's easy

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.