Hi,
I have a list of articles, for which it is supposed to be noted if an article is relevant for only a certain user group. I have three tables:
articles:
article_id (autonum) | article_name
userroles:
role_id (autonum) | role_name
context:
context_id (autonum) | context_article_id | context_role_id
As an example the article "PHP for beginners" (with article_id 3) is relevant for the user group "Students" (role_id 4) and "Teachers" (role_id 5) but NOT relevant for "Secretaries" (role_id 7)
In the database:
articles:
3 | PHP for beginners
userroles:
4 | Students
5 | Teachers
7 | Secretaries
context: (here is context_id set to 12 og 13 as an example)
12 | 3 | 4
13 | 3 | 5
My problem is that when editing a article - for example to change the relevant user groups I can't get the SQL to work. It should list ALL the exisisting user roles to chose from, and mark the ones (in a check box) that are selected for the specific/chosen article.
My attempt:
$sql_roles = mysql_query("SELECT DISTINCT r.role_id, r.role_name, c.context_id, c.context_article_id, c.context_role_id, a.article_id
FROM (userroles r LEFT JOIN context c ON c.context_role_id = r.role_id)
LEFT JOIN articles a ON a.article_id = c.context_sc_id
ORDER BY r.role_name asc");
But this lists some roles that are used by another article more than once (role_id in ()):
PICKED (5) Teachers
(5) Teachers
(7) Secretaries
(8) Super Users
(4) Students
PICKED (4) Students
How should my SQL look to accomplish this?