2016-07-27 3 views
-1

Ich möchte bekommen, wenn das 'Produkt' TV ist und die Markierung ist gut, dann möchte ich die Farbe ist rot. Ich möchte aber auch, dass jede Zeile des TV-Zeichens unabhängig von der Farbe nicht "gut" ist. Also benutze ich NOT EXISTS(), um zu überprüfen, ob die TV-Zeile 'mark' nicht 'gut' hat, aber es schlägt fehl. habe ich NOT EXISTS() falsch oder auf andere Weise benutzt? Die erwartete Ausgabe ist es, all Produkt-TV mit Markierung gut mit Farbe Rot, aber auch jedes Produkt TV Marke ist nicht ‚gut‘ regardlesDer Versuch, NOT EXISTS zu verwenden, um eine Zeile zu prüfen existiert aber

<?php 
 
$sql = $wpdb->get_var($wpdb->prepare(" 
 
SELECT product FROM table WHERE product='tv' AND amount IN (%s, %s, %s) 
 
AND mark='good' AND color=%s 
 
OR NOT EXISTS 
 
(SELECT product FROM talble WHERE product='tv' 
 
AND amount IN (%s, %s, %s) AND mark = 'good') GROUP BY product \t 
 
",$amount1,$amount2,$amount3,$color,$amount1,$amount2,$amount3)); 
 
/* 
 
product amount  color   mark 
 
tv   4  red   good 
 
tv   5   yellow  bad 
 
tv   4   black   OK 
 
tv   6  black   bad 
 
tv   4  yellow  good 
 
radio  5  yellow  good 
 
radio  6   yellow  good 
 
*/ 
 

 
?>

+1

Was ist die erwartete Ausgabe? –

+1

Ich habe versucht, Ihren Code zu bearbeiten, keine Ahnung, warum Sie Code haben, dann db-Schema, dann ein '?>'. Bitte formatiere es neu. –

+1

mischen Sie 'und' und' oder' ohne '' '' ', um die Ausführungsreihenfolge zu erzwingen. –

Antwort

0

Basierend auf Ihrer Textbeschreibung, ich glaube, die SQL wollen Sie mehr ähneln eng

select 
    product 
from 
    table 
where 
    amount in (%s, %s, %s) and 
    product='tv' and 
    ((mark='good' and color=%s) or mark!='good') 
group by 
    product 
0

Sie sollten wie

SELECT product 
FROM table 
WHERE (product='tv' 
AND amount IN (%s, %s, %s) 
AND mark='good' 
AND color=%s) 
OR NOT EXISTS 
(SELECT 1 FROM talble WHERE product='tv' 
AND amount IN (%s, %s, %s) AND mark = 'good') 
GROUP BY product 
unter

Ändern Ihrer Anfrage prüfen
Verwandte Themen