I hope the title somehow makes sense.
I’ve got 3 tables in DB. First one holds product_variant records, second one holds the label records and the third one is the ref table in between.
product_variant: (id, productId, serviceProviderId).label: (id,label).product_variant_label_ref: (label_id,variant_id).
Input parameters for my query are: productType, serviceProviderId and set of labels and
my aim is to select rows in product_variant table based on the input parameters.
This is my query in hibernate xml file:
SELECT v.*
FROM product_variant v
INNER JOIN product_variant_label_ref r ON v.id = r.variant_id
INNER JOIN product_product p ON p.productType = :productType
INNER JOIN product_label l ON l.id = r.label_id
WHERE v.product_id = p.id
AND v.serviceprovider_id = :serviceProviderId
AND l.label in (:labels)
Problem is that with IN operator I am getting disjunctive OR selection and I want AND in between. If I specify different labels as an input, I want only those product_variant rows which have a relation to all of them through ref table. If one of the input labels doesn’t belong to any product_variant, query should not return anything. Could anyone help me to change my query?
You need to count the rows returned for each product, and match it to the number of items in your labels parameter
If you have, for example, 3 labels in your labels parameter…