2

I have following table given below:

id_attribute  id_product_attribute
1             1
13            1
4             2
13            2

I want to fetch id_product_attribute which have id_attribute 1 and 13.
My query is given below:

SELECT id_product_attribute 
  FROM ps_product_attribute_combination 
 WHERE id_product_attribute = 1 
   AND id_attribute IN (1,13) 
 GROUP 
    BY id_product_attribute  

The above query is also returning the id_product_attribute 2 as well instead of only 1. How can I achieve this?

Can anyone please help to resolve this problem?
Thanks in Advance!

Martin
  • 22,212
  • 11
  • 70
  • 132
Dhirender
  • 604
  • 8
  • 23

1 Answers1

1

These links may help you:

Matching all values in IN clause

And

MySQL in-operator must match all values?

From the above you can then use:

SELECT id_product_attribute 
  FROM ps_product_attribute_combination 
 WHERE id_product_attribute = 1 
   AND id_attribute IN ('1','13') 
 GROUP 
    BY id_product_attribute  
    HAVING COUNT(DISTINCT ps_product_attribute_combination.id_attribute) = 2

So it will return results which are IN both id_attribute but also appear in id_attribute column specifically twice (using the Having clause).


Alternatively:

Your query states that :

SELECT id_product_attribute 
  FROM ps_product_attribute_combination 
 WHERE id_product_attribute = 1 

So really you're always selecting id_product_attribute = 1. So MySQL result will always be <id_product_attribute value>

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132