Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple conditional check with multiple column in same row Mysql

I want to select data from the below table as per matching of two columns with multiple conditional checks.

enter image description here

I want to check conditions like the following.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When product_category_variants_id = 1 and product_category_variants_value_id = 2 and again product_category_variants_id = 2 and product_category_variants_value_id = 5. Then I need to get out put 8,7 and 6.

I tried the following MySQL Query to archive the above result.

SELECT DISTINCT `product_id` FROM `product_variants` WHERE (`product_category_variants_id` = 1 AND `product_category_variants_values_id` = 2) AND (`product_category_variants_id` = 2 AND `product_category_variants_values_id` = 5);

>Solution :

You need an aggregation query here, as the two conditions in your WHERE clause can never simultaneously be true for any given record.

SELECT product_id
FROM product_variants
GROUP BY product_id
HAVING SUM(product_category_variants_id = 1 AND product_category_variants_values_id = 2) > 0 AND
       SUM(product_category_variants_id = 2 AND product_category_variants_values_id = 5) > 0;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading