Hallo

ik heb een stukje sql:
<?php
$selecteer_news_sql = "
SELECT
n.titel,
n.datum,
c.naam AS auteur,
l.naam AS cat,
n.actief,
n.id AS id,
COUNT(comments.id) AS reacties
FROM
nieuws AS n,
cats AS c,
leden AS l,
comments
WHERE
n.user_id = l.id AND
n.cat = c.id
".$select_user_news."
".$zoek_sql."
".$actief_sql."
GROUP BY
(n.id)
ORDER BY
n.datum DESC,n.id DESC
LIMIT
".$limit;
?>

Hiermee haal ik dus wat gegevens uit verschillende tabellen...
Nu zit het probleem in het tellen van het aantal rijen uit de tabel reacties per nieuwsitem. Met de sql die ik nu heb geeft hij alle rijen terug... dus het aantal rijen in die tabel totaal, en ik wil per nieuwsitem.

Als ik nog comments.news_id = n.id bij zet geeft hij een error:
<?php
$selecteer_news_sql = "
SELECT
n.titel,
n.datum,
c.naam AS auteur,
l.naam AS cat,
n.actief,
n.id AS id,
COUNT(comments.id) AS reacties
FROM
nieuws AS n,
cats AS c,
leden AS l,
comments
WHERE
n.user_id = l.id AND
n.cat = c.id
comments.news_id = id
".$select_user_news."
".$zoek_sql."
".$actief_sql."
GROUP BY
(n.id)
ORDER BY
n.datum DESC,n.id DESC
LIMIT
".$limit;
?>

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 'comments.news_id = id GROUP BY (n.id) ORDER BY n.datum DESC,n.i' at line 17

Ik heb al een hele tijd geprobeerd enzo... maar ik kom er niet echt uit ...

Groeten,
Arian
comments.news_id = id

moet id daar dan niet n.id zijn?
Maakt niet uit... doet het alle twee niet ...
Even een gokje, maar moet dit:


n.datum DESC,n.id DESC 


niet dit worden:


n.datum, n.id DESC

??
Bij de bovenste sql van mij geeft hij geen error alleen telt hij alle rijen uit de tabel reacties... (dus hij geeft overal even veel reacties neer)
<?php
$selecteer_news_sql = "
SELECT
n.titel,
n.datum,
c.naam AS auteur,
l.naam AS cat,
n.actief,
n.id AS id,
COUNT(comments.id) AS reacties
FROM
nieuws AS n,
cats AS c,
leden AS l,
comments
WHERE
n.user_id = l.id AND
n.cat = c.id AND
comments.news_id = n.id
".$select_user_news."
".$zoek_sql."
".$actief_sql."
GROUP BY
(n.id)
ORDER BY
n.datum DESC,n.id DESC
LIMIT
".$limit;
?>

En zo?

(was AND vergeten bij je where die je extra toevoegde)
Oke hij werkt!!

MAAR, nu worden alleen de nieuwsitems weergegeven met >= 1 reacties...
Hij moet ze allemaal weergeven, ook die met 0 reacties. Dus als er geen reacties zijn dat het 'reacties' gewoon nul is...

Reageren