2013-10-26 101 views
0

我想選擇已在此查詢nbre> 0項目:mysql的SELECT COUNT其中計數> 0

SELECT c.id, c.nom, c.photo, 
    (SELECT count(*) 
    FROM touist_materiel 
    WHERE cat=c.id AND marque=7 AND actif=1 
    ) as nbre 
FROM touist_materiel_cat c 
INNER JOIN touist_materiel m ON m.cat=c.id 
WHERE nbre > 0 
GROUP BY m.cat 
ORDER BY c.nom 

此查詢不工作,我怎麼會有「裏nbre> 0 「工作? 謝謝

+0

這是不容易理解,你想擁有的東西。你可以製作一個平臺,並準確解釋你想要的東西嗎? – kmas

回答

0

也許嘗試沒有子查詢?

SELECT c.id, c.nom, c.photo, count(m.*) AS nbre 
FROM touist_materiel_cat c 
INNER JOIN touist_materiel m 
    ON m.cat=c.id AND m.marque=7 AND m.actif=1 
GROUP BY m.cat 
ORDER BY c.nom 

隨着INNER JOIN,我們總是有計數(M *)> 0

0
SELECT tmc.id, tmc.nom, tmc.photo, count(tm.*) AS nbre 
    FROM touist_materiel_cat tmc 
    INNER JOIN touist_materiel tm 
    ON tm.cat=tmc.id 
    WHERE nbre > 0 
    AND tm.marque=7 
    AND tm.actif=1 
    GROUP BY tm.cat 
    ORDER BY tmc.nom; 
+1

在我看來好... – kmas