...

View Full Version : adding 2 counts to an existing query



DionDev
03-22-2009, 11:15 PM
i need to add the following 2 queries:



$query = mysql_query("SELECT COUNT(*) FROM `peers` WHERE `hash` = RTRIM('$hash) AND `left` = 0 AND `expire_time` > NOW()");
$query = mysql_query("SELECT COUNT(*) FROM `peers` WHERE `hash` = RTRIM('$hash) AND `left` > 0 AND `expire_time` > NOW()");

to this query:



$sql = mysql_query("select t.id, t.uploader_id, s.memberName, t.category_id, c.name, t.hash, t.title, t.file_name, t.file_size, t.rating, t.date_uploaded
from thefiles as t, smf_members as s, categories as c
where t.uploader_id = s.ID_MEMBER and t.category_id = c.id
order by t.id desc
limit 50") or die(mysql_error());

i created that query myself, but i have no idea how to add the counts to it so its all in 1 nice query. can a guru help?

Old Pedant
03-23-2009, 12:36 AM
First of all, I don't see how those first two queries can possibly work.

You are missing an apostrophe in each
$query = mysql_query("SELECT COUNT(*) FROM `peers` WHERE `hash` = RTRIM('$hash) AND `left` = 0 AND `expire_time` > NOW()");

It should be
RTRIM('$hash')

Secondly, if you want to get *both* counts in a single record, then you can't use COUNT, actually.

Back in a bit after I look at the other query.

Old Pedant
03-23-2009, 12:40 AM
Sorry, I don't see the relationship between the bigger query and the first two.

Each of the first two get a *single* value for the entire peers table.

The third query is getting up to 50 records from completely unrelated tables. I can't see any good reason (or way) to integrate the first two with it.

I think you should just leave things alone.

You could consilidate the first two queries into one, but I doubt that will make a huge performance difference.

************

OH WAIT!

Are you wanting to get those two COUNTs on a *PER* t.hash basis, where t.hash is gotten as it is in the 3rd query???

Now *that* would make sense.

DionDev
03-23-2009, 01:23 AM
Sorry, I don't see the relationship between the bigger query and the first two.

Each of the first two get a *single* value for the entire peers table.

The third query is getting up to 50 records from completely unrelated tables. I can't see any good reason (or way) to integrate the first two with it.

I think you should just leave things alone.

You could consilidate the first two queries into one, but I doubt that will make a huge performance difference.

************

OH WAIT!

Are you wanting to get those two COUNTs on a *PER* t.hash basis, where t.hash is gotten as it is in the 3rd query???

Now *that* would make sense.

yes man. i want them to count where the peers.hash = thefile.hash, and where left is > 0 and = 0 for 2 different counts.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum