I have a java method that receives a list of strings as a parameter and I want to get the row(s) that matches exactly all the values of the list (no more, no less).
I have two tables (a Cases table and a Products table) that has a many-to-many relationship.
CASES_TABLE
ID CASES_COMMENT ...
1 Some comment ...
2 Some comment2 ...
3 Some comment3 ...
... ...
PRODUCTS_TABLE
ID PRODUCT_CODE
1 VoIP
2 IPTV
3 PSTN
... ...
CASES_PRODUCTS_TABLE
CASE_ID PRODUCT_ID
1 1
2 1
2 2
2 3
3 1
3 2
... ...
I'm using Oracle as the rdbms.
For instance:
Given the list:
List<String> products = Stream.of("VoIP", "IPTV","PSTN").collect(Collectors.toList());;
And the method String getSomeValue (List products);
What I'd want get is the case with the id 2, because that's the one that matches exactly all the values (not the case with the id 1 because it matches only one value, and not the case with the id 2 because it matches only two values).
I've read about relational division, but I thinks it doesn't solve my problem. I've also try with the IN condition but it performs an 'OR' and I want my query to filter exactly by all values without leaving one left or getting a row that has a value that wasn't present on the list.
I tried the following:
SELECT CASES_COMMENT
FROM CASES_TABLE CT JOIN CASES_PRODUCTS_TABLE CPT
ON CT.ID = CPT.CASE_ID
JOIN PRODUCTS_TABLE
ON PT.ID = CPT.PRODUCT_ID
WHERE PRODUCT_CODE IN ('VoIP', 'PSTN', 'IPTV');