View Single Post
Men jeg skal ikke bare telle, jeg skal telle antall av relaterte i en relatert tabell:

select forumID,count(forumID) FROM innlegg GROUP BY forumID;
+---------+----------------+
| forumID | count(forumID) |
+---------+----------------+
| 2 | 2 |
| 10 | 2 |
| 11 | 1 |
| 13 | 2 |
| 17 | 2 |
+---------+----------------+
5 rows in set (0.00 sec)

mysql> select id from forum WHERE ( fjernet IS NULL or fjernet="0" );
+----+
| id |
+----+
| 7 |
| 8 |
| 10 |
| 11 |
| 12 |
| 13 |
| 17 |
| 18 |
+----+

Ønsker én query istede for to resultater av eksempelet over bør bli:

+----+----------------+
| id | count(forumID) |
+----+----------------+
| 7 | 0 |
| 8 | 0 |
| 10 | 2 |
| 11 | 1 |
| 12 | 0 |
| 13 | 2 |
| 17 | 2 |
| 18 | 0 |
+----+----------------+

[edit]
Denne gir count > 0
---
select f.id,i.forumID,count(forumID) FROM forum AS f, innlegg AS i WHERE i.forumID=f.id and ( f.fjernet IS NULL or f.fjernet="0" ) GROUP BY i.forumID;
[/edit]
Sist endret av fxxked; 5. desember 2003 kl. 23:03.