i have three tables: A, B and C, B is the table necessary to break the many to many relationship:
table A table B table C
------- ------- -------
id id id
.... id_A ...
.... id_C ...
and i have two multiselect html object two display the results, in the second one i need the intersection of both, that is a simple inner join.
the problem is the first one, i need to display there all items are available, so not the same items could be displayed in both.
in a first time, in order to fill the first multiselect i just do a table A left join with table B where the B.id is null, that was ok:
select * from tableA as A
left join tableB as B on A.id = B.id_A
where B.id_A is null
but after that i realized that there is a problem, because if there another relation AC, this relation will appear in both multiselect, so i tried:
select * from tableA as A
left join tableB as B on A.id = B.id_A
where B.id_A is null or B.id_C != $id
and doesn’t work, i need a way to display en the first multiselect only the available and not repeat options every time i change the id_C
i hope somebody can help me 🙂
try this,
SELECT tableC.field1, tableC.field2
FROM tableC
LEFT JOIN tableB ON tableC.ID = tableB.ID_C
AND tableB.ID_A = $ID_A
WHERE tableB.ID_A IS NULL
GROUP BY tableC.field1, tableC.field2