yes, you meant a multi-level sort
(it is potentialy misleading to use the term "grouped by" in this context, as GROUP BY in SQL has a completely different function from sorting)
okay, a client can have one or more projects -- this is a one-to-many relationship
a project can have one or more documents -- another one-to-many relationship
but a document can belong to multiple categories and a category can have multiple documents -- a many-to-many relationship
and a document can belong to multiple sections and a section can have multiple documents -- also a many-to-many relationship
unfortunately, pulling everything together in a single query is not as simple as it would appear
your illustration shows sections within categories, but unfortunately there is no relationship in the tables to support this
my advice is to see if you can actually load the data into your tables that reflects this, and i think you'll see it's not possible
so you'll need to rethink the categories and sections part of the design
as for the unnecessary columns, here's what the many-to-many tables should look like (assuming you need both of these tables) --
Code:
CREATE TABLE document_categories
( document_id INTEGER UNSIGNED NOT NULL
, category_id INTEGER UNSIGNED NOT NULL
, PRIMARY KEY (document_id, category_id)
);
CREATE TABLE document_sections
( document_id INTEGER UNSIGNED NOT NULL
, section_id INTEGER UNSIGNED NOT NULL
, PRIMARY KEY (document_id, section_id)
);
the auto_increment columns are unnecessary and counter-productive
Bookmarks