Hi,
I'm pretty fresh to database handling, but now completely without experience. However, I have gotten stuck on a problem. I need to formulate an SQL query that returns all articles that are compatible to a set of other articles (of arbitrary size). The query is to be generated by a script in an application to search for articles where the user can input a list of articles that any found articles should usable(compatible) with.
So for a list of article numbers A, B, ... , N, the question is:
"Give me all articles that are compatible with A and B and ... and N"
The question concerns only one table;
Compatible
artOne
artTwo
Each record in Compatible represents a compatibility relation so that articles A and B are compatible iff there is a record with article number A in one column and B in the other. NB the order doesn't make any difference for the compatibility.
Now, given a list of articles, I want to be able to generate a query that returns all articles that are compatible.
For example, consider the table
Compatible
A B ---- 1 2 3 1 3 4
If I wanted all articles that are compatible with [1], the query would return [2, 3].
The query generated by the list [2, 3] will return [1].
Whilst the query generated from the list [1, 3] generates an empty list.
Admittedly, this is probably not the best way to tackle the problem so I welcome any better solutions as well. I reckon this type of questions require some type of subqueries, a subject which I am yet to master.
So, my question is -- Is there any way to model the database so this particular problem is solved in an easier manner, or in any case, could someone help me formulating the query and how it changes with the variable amount of input. Any pointers to reading on the subject is also very much welcome.
Many thanks
Marco