RE: [PHP-DB] Design conundrum...
| From: | Kristian Duske | Date: | Sat, 23 Jun 2001 11:29:48 +0000 |
| Subject: | RE: [PHP-DB] Design conundrum... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-54973@lists.php.net to get a copy of this message | ||
> The problem involves 2 tables, one of editors and one of categories, both
> have unique ID numbers. Each editor can have authority over an arbitrary
> number of categories, and conversely, each category can have an arbitrary
> number of editors. So what is the best way to represent this in the DB?
> (Which is PostgreSQL 7 BTW)
This is a so-called N:N relation, and this usually calls for a third table
to store the cross-relations:
table editors
id, name, email
table categories
id, name
table ediors_categories
editor (id of editor)
category (id of category)
Now if you want to give an editor authority over a category, you would
insert a new record into editors_categories with the respective ids of the
editor and the category.
If you want to select all categories that belong to a certain author, you
would do this:
SELECT c.* FROM categories c LEFT JOIN editors_categories ed ON c.id =
ed.category WHERE ed.editor = '" . $editor_id . "'"
This selects all category records and joins them with the editors_categories
table, then filtering the resulting recordset by the editor id stored in the
editors_categories table.
Hope this helps
Kristian