Dear SQL gurus,I am trying to write some script and can not figure out a (supposedly simple) trick, please help!// C - Categories, P - Parameters, CP_Link - linkage table between C and P// Basically, it is a list of features belonging to a particular category.DROP TABLE [C];CREATE TABLE [C] ( ID_C Autoinc, Name varchar(32), PRIMARY KEY( ID_C ));DROP TABLE [P];CREATE TABLE [P] ( ID_P Autoinc, Name varchar(32), PRIMARY KEY( ID_P ));DROP TABLE [CP_Link];CREATE TABLE [CP_Link] ( ID Autoinc, FK_ID_C integer, // foreign keys FK_ID_P integer, PRIMARY KEY( ID ));INSERT INTO [C] ( Name ) VALUES ( 'boys' ); // 1INSERT INTO [C] ( Name ) VALUES ( 'girls' ); // 2INSERT INTO [C] ( Name ) VALUES ( 'snakes' ); // 3INSERT INTO [P] ( Name ) VALUES ( 'leg' ); // 1INSERT INTO [P] ( Name ) VALUES ( 'hand' ); // 2INSERT INTO [P] ( Name ) VALUES ( 'beard' ); // 3INSERT INTO [P] ( Name ) VALUES ( 'tail' ); // 4INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,1 ); // boysINSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,2 );INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 1,3 );INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 2,1 ); // girlsINSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 2,2 );INSERT INTO [CP_Link] ( FK_ID_C, FK_ID_P ) VALUES ( 3,4 ); // snakes//-------------------------------------------------------SELECT P.Name FROM C,P,CP_LinkWHERE (FK_ID_C=ID_C) AND (FK_ID_P=ID_P) AND (C.Name='girls');// this will result in:// leg// hand// we have category 'girls' selected, which has two features linked. All works fine.// now a bit more complex:SELECT DISTINCT P.Name FROM C,P,CP_LinkWHERE (FK_ID_C=ID_C) AND (FK_ID_P=ID_P) AND (C.Name IN ('boys','girls'));// so we have more than one category as input parameter, this will result in:// leg// hand// beard// so this is a joint list of all features listed.
OK, this works fine, but I want to get a list of parameters (from P table) which holds only those items that linked to all categories requested. In other words, I want only 'leg' and 'hand', but not the 'beard', because 'girls' not linked to 'beard'.Only common features for all of categories listed "IN (,,,)" wanted.thank you!