Russ -
I have some questions for you. For each item in Drug_Class_Class_Interactions, does both Class1 AND Class2 have to be in the list can Class1 OR Class2 be in the list? If you are looking for Class1 AND Class2 then just the first part of the query without the Union should get you everything you need without any duplicates. However, if, for each item in Drug_Class_Class_Interactions, either Class1 or Class2 have to be in the list then you have a different situation. With the Class1 or Class2 situation you can have three different scenarios:
a. Class1 in list and Class2 in list
b. Class1 in list and Class2 not in list
c. Class1 not in list and Class2 in list
Would scenarions b and c ever happen?
You can also try the query below. Right now I can't figure out if you'll get duplicates or not. If I'm thinking clearly you should not get any duplicates.
SELECT DRUG_CLASSES1.NAME AS DRUG1, DRUG_CLASSES2.NAME
AS DRUG2, DRUG_CLASS_CLASS_INTERACTIONS.*
FROM DRUG_CLASS_CLASS_INTERACTIONS,
DRUG_CLASSES AS DRUG_CLASSES1, DRUG_CLASSES AS
DRUG_CLASSES2
WHERE (CLASS1 IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24) OR
CLASS2 in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24) AND
CLASS1=DRUG_CLASSES1.ID) AND
(CLASS2=DRUG_CLASSES2.ID);
If I'm not thinking clearly and you do get duplicates, then you can try the union query below where 1) the first select pulls in all the items where Class1 is in the list (covers scenario a and b above) and 2) the second select would pull in items where class2 is in the list, but class1 was not in the list (covers scenario c above). So your query should look like:
SELECT DRUG_CLASSES1.NAME AS DRUG1, DRUG_CLASSES2.NAME
AS DRUG2, DRUG_CLASS_CLASS_INTERACTIONS.*
FROM DRUG_CLASS_CLASS_INTERACTIONS,
DRUG_CLASSES AS DRUG_CLASSES1, DRUG_CLASSES AS
DRUG_CLASSES2
WHERE (CLASS1 IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24) AND CLASS1=DRUG_CLASSES1.ID) AND
(CLASS2=DRUG_CLASSES2.ID)
UNION
SELECT DRUG_CLASSES1.NAME AS DRUG1, DRUG_CLASSES2.NAME AS
DRUG2, DRUG_CLASS_CLASS_INTERACTIONS.*
FROM DRUG_CLASS_CLASS_INTERACTIONS,
DRUG_CLASSES AS DRUG_CLASSES1,
DRUG_CLASSES AS DRUG_CLASSES2
WHERE (CLASS1 NOT IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,24) AND CLASS1=DRUG_CLASSES1.ID) AND (CLASS2 IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,17,19,24) AND CLASS2=DRUG_CLASSES2.ID)
ORDER BY SEVERITY DESC;
There is an assumption I've been making that may be incorrect. I'm assuming that you will always have something in both Class1 and Class2. If this is NOT the case, then both queries above will leave out the results of the items where Class1 or Class2 are blank.
Finally, I'm confused by the union query you posted. The only difference I see in the two select statements of the query is that in the first one you get the description of Class1 from the first copy of the Drug_Classes table and the description of Class2 form the second copy of the Drug_Classes table and in the second select statement it is the other way around. That's why the names are inverted between your first and last rows in the example above. I would think that you are still getting duplicates for each of your entries, but the duplicates all have the names inverted.
Let me know how this works out.