2

I need to perform a SELECT on the PRODUCTS table that only returns result if the conditions of the PRODUCTS_DETAILS table are true

follow the sql:

select p.* from products p
join products_details pd
on p.id = pd.id_product
WHERE pd.details = 'quadra mar'
AND pd.details = 'prédio novo'

however when I put two or more "AND" it does not return results

objective: search for all products that have the "where" details

8
  • try OR instead of AND Commented Jan 20, 2022 at 14:52
  • "OR" is not my goal, I only need it to meet the conditions 'QUADRA MAR' and 'PRÉDIO NOVO', if I use "OR", it will search even if ID 2 does not have 'PRÉDIO NOVO' Commented Jan 20, 2022 at 14:54
  • I guess it's not possible for pd.details to be 'quadra mar' and 'prédio novo' at the same time. Commented Jan 20, 2022 at 14:55
  • @stersym,is there any other alternative? I'm a beginner in MYSQL Commented Jan 20, 2022 at 14:56
  • Alternative to what. You are asking for something impossible. (Unless i'm misundestanding something). Is there any way that your Column Details would have the values 'quadra mar' AND 'prédio novo' at the same time in the same row? Show us an example of your table. Commented Jan 20, 2022 at 14:58

1 Answer 1

1

Try,

SELECT p.* 
FROM products p
INNER JOIN product_details pd ON p.id = pd.id_product 
                              AND pd.details IN ('quadra mar', 'prédio novo');

Take a look in your WHERE clause, I think an OR condition will fix your issue too.

... WHERE pd.details = 'quadra mar' OR pd.details = 'predio novo'

And if you want only products that contains all the conditions in where clause, you can try:

SELECT pd.id_product, pd.details 
FROM products p
INNER JOIN product_details pd ON p.id = pd.id_product 
                              AND pd.details IN ('quadra mar', 'prédio novo') 
GROUP BY pd.id_product, pd.details 
HAVING COUNT(*) > 1;
Sign up to request clarification or add additional context in comments.

1 Comment

@rodrigão, me salvou, seu último código me serviu perfeitamente, segue: select p.* from products p join products_details pd on p.id = pd.id_product AND pd.details IN ('quadra mar','prédio novo','frente mar') GROUP BY p.id HAVING count(*) > 2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.