2010-12-13 15 views
1

Ich habe eine Arbeits Abfrage alsMySQL-Abfrage, wo Bedingung Problem

SELECT p.id, 
      p.name AS ProductName, 
      count(DISTINcT s.salesid) as Sales, 
      Count(DISTINCT l.linkid) as Links 
    FROM products p 
LEFT JOIN sales s ON p.id=s.productid 
LEFT JOIN links l ON p.id=l.productid 
GROUP BY p.id 

Nun, ich brauche nur die Datensätze, bei denen entweder Verkäufen auf 0 oder Links nicht gleich ist nicht gleich 0 oder beide nicht gleich 0

Wie kann ich das erreichen?

Antwort

3

In einer HAVING-Klausel

SELECT p.id, p.name AS ProductName, 
count(DISTINcT s.salesid) as Sales, Count(DISTINCT l.linkid) as Links 
FROM products p 
LEFT JOIN sales s ON p.id=s.productid 
LEFT JOIN links l ON p.id=l.productid 
GROUP BY p.id 
HAVING Sales > 0 OR Links > 0 
+0

es funktioniert, dank –

Verwandte Themen