22. januar 2007 - 22:41Der er
22 kommentarer og 1 løsning
Hvordan fjerner jeg poster hvor count = 0
hejsa,
Jeg vil gerne ændre denne query, så den ikke medtager poster, hvor 'antal' = 0:
SELECT indhold.id, indhold.overskrift, count(indhold_menupunkter.id) as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status=1 GROUP BY indhold.id
Denne side indeholder artikler med forskellige perspektiver på Identity & Access Management i private og offentlige organisationer. Artiklerne behandler aktuelle IAM-emner og leveres af producenter, rådgivere og implementeringspartnere.
SELECT indhold.id, indhold.overskrift, count(indhold_menupunkter.id) as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status=1 and 'antal' != 0 GROUP BY indhold.id
SELECT indhold.id, indhold.overskrift, count(indhold_menupunkter.id) as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status=1 and 'antal' != 0 GROUP BY indhold.id
Giver 0 rækker. Min oprindelige query gav ca. 50 rækker....??!?
SELECT indhold.id, indhold.overskrift, count(indhold_menupunkter.id) as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status=1 and 'antal' <> 0 GROUP BY indhold.id
SELECT indhold.id, indhold.overskrift, count( indhold_menupunkter.id ) AS antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status =1 AND antal !=0 GROUP BY indhold.id
SELECT indhold.id, indhold.overskrift, count(indhold_menupunkter.id) as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.status=1 and antal <> 0 GROUP BY indhold.id
nu ved jeg ikke om jeg har overset noget i denne, men det kan gøres på denne måde ellers..
SELECT indhold.id, indhold.overskrift FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = menupunktid WHERE indhold.id IN (SELECT indhold_menupunkter.id, count(indhold_menupunkter.id) as antal FROM indhold_menupunkter WHERE antal <> 0) indhold.status=1 GROUP BY indhold.id
Du har ret mht. indhold.id = menupunktid er forkert, den bør se således ud:
SELECT indhold.id , indhold.overskrift , count(indhold_menupunkter.id )as antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = indhold_menupunkter.menupunktid WHERE indhold.status =1 AND antal > 0 GROUP BY indhold.id
Anyway, så virker den fortsat ikke, og jeg får også fejl #1064 (syntax fejl) på Kalp's nyeste forslag.
Jeg er sgu lidt på r.... med denne her. Håber der findes en løsning. Giver naturligvis gerne flere point - 200 - for en løsning.
SELECT indhold.id, indhold.overskrift FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = indhold_menupunkter.menupunktid WHERE indhold.id IN (SELECT indhold_menupunkter.menupunktid, count(indhold_menupunkter.menupunktid) as antal FROM indhold_menupunkter WHERE antal <> 0) indhold.status=1 GROUP BY indhold.id
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'indhold . status = 1 GROUP BY indhold . id'
------------------------------------------------------------------- SELECT SQRT(a*b) AS root FROM tbl_name GROUP BY root HAVING root > 0; SELECT id, COUNT(*) AS cnt FROM tbl_name GROUP BY id HAVING cnt > 0; SELECT id AS 'Customer identity' FROM tbl_name;
Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.
For example, the following query is illegal: SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;
The WHERE statement is executed to determine which rows should be included in the GROUP BY part, whereas HAVING is used to decide which rows from the result set should be used. -------------------------------------------------------------------
Mon det er HAVING, som kan hjælpe mig? Jeg forsøger at lege lidt med det, vender tilbage.
SELECT indhold.id, indhold.overskrift, count( indhold_menupunkter.id ) AS antal FROM indhold LEFT JOIN indhold_menupunkter ON indhold.id = indhold_menupunkter.menupunktid WHERE indhold.status =1 GROUP BY indhold.id HAVING antal <>0
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.