2016-08-16 5 views
2

Ich habe folgende Tabelle unten angegeben:Fest mit Mysql IN Operator

id_attribute id_product_attribute 
1    1 
13   1 
4    2 
13   2 

ich id_product_attribute holen will, die id_attribute1 und 13 hat.
Meine Frage ist unten angegeben:

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 

Die obige Abfrage auch die id_product_attribute2 auch statt nur 1 zurückkehrt. Wie kann ich das erreichen?

Kann jemand bitte helfen, dieses Problem zu lösen?
Vielen Dank im Voraus!

+1

Es ist nicht möglich. Sie haben möglicherweise andere Daten als die, die Sie eingegeben haben. – Shaharyar

+1

HAVING COUNT (*) = 2 (und entfernen Sie die WHERE-Klausel) – Strawberry

+0

Mögliche Duplikat von [Übereinstimmen aller Werte in IN-Klausel] (http://stackoverflow.com/questions/11636061/matching-all-values-in-in-in- Klausel) – CBroe

Antwort

1

Diese Links können Ihnen helfen:

Matching all values in IN clause

Und

MySQL in-operator must match all values?

Aus der obigen Sie dann verwenden können:

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 wird es Ergebnisse zurück, die sind IN sowohl id_attribute, sondern erscheinen auch in id_attribute Spalte speziell zweimal (unter Verwendung der Having Klausel).


Alternativ:

Ihre Anfrage lautet:

SELECT id_product_attribute 
    FROM ps_product_attribute_combination 
WHERE id_product_attribute = 1 

Also wirklich sind Sie immer id_product_attribute = 1 auswählen. So wird MySQL Ergebnis immer <id_product_attribute value>