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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.