You have, I believe, two tables containing entities. The entities are employee
and report
.
These entities have two different many-to-many relationships: author
and attendee
.
So your tables are these
employee report
-------- -----
employee_id (PK) report_id (PK)
surname title
givenname releasedate
whatever whatever
Then you have two many:many relationship tables with the same columns as each other. One is author
and the other is attendee
.
author / attendee
------
employee_id PK, FK to employee.employee_id
report_id PK, FK to report.report_id
Notice the compound (two-column) primary keys.
+---------------------+\ /+-------------+\ /+-----------------------+
| +-----+ author +-----+ |
| |/ \+-------------+/ \| |
| employee | | report |
| | | |
| |\ /+-------------+\ /| |
| +-----+ attendee +-----+ |
+---------------------+/ \+-------------+/ \+-----------------------+
\ /
----- means a many-to-many relationship
/ \
When you determine an employee is an attendee for a certain report, you insert a row into the attendee table with the correct employee and report.
If you want, for example, all authors for each report you can do this sort of thing:
SELECT r.title, r.releasedate,
GROUP_CONCAT(e.surname ORDER BY e.surname SEPARATED BY ',')surnames
FROM report r
LEFT JOIN author a ON r.report_id = a.report_id
LEFT JOIN employee e ON a.report_id = e.report_id
GROUP BY r.title, r.releasedate
ORDER BY r.releasedate DESC
The LEFT JOIN
operations allow your query to find reports that have no authors. Ordinary inner JOIN
operations would suppress those rows from your result set.
There is a limitation with this strict E:R design. For many kinds of reports, (scientific papers for example) the order of authors is critically important. (You want to start an academic food fight? List the authors of a paper in the wrong order.)
So you author table might also contain an ordinal value.
author
------
employee_id PK, FK to employee.employee_id
report_id PK, FK to report.report_id
ordinal INT
and your report query would contain this line.
GROUP_CONCAT(e.surname ORDER BY e.ordinal SEPARATED BY ',')surnames