RE: [PHP-DB] Design conundrum...

From: Date: Sat, 23 Jun 2001 11:52:18 +0000
Subject: RE: [PHP-DB] Design conundrum...
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-9762@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 editors_categories ec LEFT JOIN categories c 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

« previous php.db (#9762) next »