MySQL Query

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I want to have a field where I store a list of what categories that
particular row falls under, I don't know which format to do or how I
would query it.

For example, row1 might belong to categories 1, 3 & 7 - I'd want to be
able to query all rows belonging to 3 and get row1.

Re: MySQL Query

*** escribió/wrote (26 Aug 2006 10:07:08 -0700):
Quoted text here. Click to load it

The logical approach in a normalized database would be to create a separate
table with the following fields:


id_foo - Foreign key
id_category - Foreign key

Quoted text here. Click to load it

Stored data would like like this:

1 1
1 3
1 7

Quoted text here. Click to load it

FROM categories
INNER JOIN foo_to_categories
ON categories.id_category=foo_to_categories.id_category
WHERE foo_to_categories.id_category=3

Replace "foo" with the actual object name, I figured out it won't be "row"
unless you're building a ticket selling system ;-P

P.S. Code is untested

-+ - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web:
+- Mi web de humor con rayos UVA:

Re: MySQL Query wrote:
Quoted text here. Click to load it

Answered in comp.database.mysql.

And if you're going to post to several newsgroups, please:

  - Pick appropriate newsgroups.  c.l.p isn't one of them for a SQL question
  - Cross-post your question instead of multi-posting.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline