Jump to content

[SOLVED] Wrong number of returned rows.


The Little Guy

Recommended Posts

Why does this only return one result:

 

SELECT *, t.id as tid, t.title as tTitle, COUNT(r.id) as 'replyCount' FROM 
topics t LEFT JOIN replies r ON (r.topic_id = t.id) 
LEFT JOIN users u ON (u.id = t.owner) 
WHERE t.boardID = '6'
LIMIT 30

 

 

And why does this return two results:

 

SELECT *, t.id as tid, t.title as tTitle FROM 
topics t LEFT JOIN boards b ON (t.boardID = b.id) 
LEFT JOIN users u ON (u.id = t.owner) 
WHERE t.boardID = '6'
LIMIT 30

 

I would like to use something like the first one so I can get a count, but I would like it to return the two results like in the second one. Any Help?

Link to comment
https://forums.phpfreaks.com/topic/139799-solved-wrong-number-of-returned-rows/
Share on other sites

The first one can return nonsense, since you're using an aggregate function without a proper group by clause -- this has two effects: (1) you'll only get back one row since you're group by "all" and (2) the column values will be utter garbage.

 

You may have to use a subquery.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.