tags:

views:

95

answers:

2

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

A: 

select * from article a inner join compatTbl ct on a.Id=ct.ArticleAID where ct.ArticleBID in (list of ids go here)

+1  A: 
SELECT  id
FROM    (
        SELECT  B AS id
        FROM    compat
        WHERE   A IN (list)
        UNION
        SELECT  A
        FROM    compat
        WHERE   B IN (list)
        ) q
GROUP BY
        id
HAVING  COUNT(*) = @cnt

, where @cnt is the total number of items in your list.

For this to work, you should make sure that no compatibility pair has two entries in the table (i. e. having (1, 2) and (2, 1) at once is bad).

It's best to have two constraints: one ensuring that the pair is unique, the other one checking that the article with the least id to be first:

ALTER TABLE compat ADD CONSTRAINT ux_compat_ab UNIQUE (A, B)
ALTER TABLE compat ADD CONSTRAINT cc_compat_order CHECK (A < B)

If you do this, you can replace UNION with more efficient UNION ALL:

SELECT  id
FROM    (
        SELECT  B AS id
        FROM    compat
        WHERE   A IN (list)
        UNION ALL
        SELECT  A
        FROM    compat
        WHERE   B IN (list)
        ) q
GROUP BY
        id
HAVING  COUNT(*) = @cnt
Quassnoi
That solved it perfectly! Thanks a ton!----Marco
Nubsis