Jump to content

Recommended Posts

Hi, does anyone know if it is possible to do a multiple count inside one query? Like say you want to separately count 3 different fields. The first one would be easy, to count the total, use mysql_num_rows, but the other two I'm not sure... like how many of those rows match this fields criteria and how many rows meet that fields criteria.
Link to comment
https://forums.phpfreaks.com/topic/5634-multiple-count-inside-one-query/
Share on other sites

[!--quoteo(post=357784:date=Mar 23 2006, 10:39 PM:name=webwired)--][div class=\'quotetop\']QUOTE(webwired @ Mar 23 2006, 10:39 PM) [snapback]357784[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi, does anyone know if it is possible to do a multiple count inside one query? Like say you want to separately count 3 different fields. The first one would be easy, to count the total, use mysql_num_rows, but the other two I'm not sure... like how many of those rows match this fields criteria and how many rows meet that fields criteria.
[/quote]

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] [color=blue]COUNT[/color](1fieldName), [color=blue]COUNT[/color](2fieldName), [color=blue]COUNT[/color](3fieldName) [color=green]FROM[/color] [color=orange]tblName[/color] [!--sql2--][/div][!--sql3--]

They're all going to, actually, be the same. SO.

Really what you're looking for.. Is one query, with 2 conditions based on other fields right?

You need to explain your question a bit more [=
[!--quoteo(post=358012:date=Mar 24 2006, 07:02 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 24 2006, 07:02 PM) [snapback]358012[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] [color=blue]COUNT[/color](1fieldName), [color=blue]COUNT[/color](2fieldName), [color=blue]COUNT[/color](3fieldName) [color=green]FROM[/color] [color=orange]tblName[/color] [!--sql2--][/div][!--sql3--]

They're all going to, actually, be the same. SO.
[/quote]

Not necessarily. COUNT(colname) only counts non-null values.
Thank you, but actually what I was thinking and what I was typing didn't come out the same... What I really meant to ask is take for instance you have a field called link and it records hits from different advertisers that you use... i.e. advertising company 1 you give them your URL [a href=\"http://url.com/index.php?link=advertiser1\" target=\"_blank\"]http://url.com/index.php?link=advertiser1[/a] So and so forth... So now you want to do a query to get the total number of hits, the total numbers of those that are from advertiser1 and the total number of hits from advertiser2. I'll show the code I'm using right now to accomplish this task,... Now don't laugh.

[code]
$bdate = date('Y-m-d 00:00:00', mktime(0, 0, 0, $_POST[month], $_POST[day],  $_POST[year]));
$edate = date('Y-m-d 23:59:59', mktime(0, 0, 0, $_POST[month], $_POST[day],  $_POST[year]));
$query1 = mysql_query("select link from refererlog  WHERE datetime BETWEEN '$bdate' AND '$edate'") or die(mysql_error());
$row = mysql_fetch_assoc($query1);
$total = mysql_num_rows($query1);
$query2 = mysql_query("select link from refererlog where link = 'advertiser1'" AND datetime BETWEEN '$bdate' AND '$edate') or die(mysql_error());
$row = mysql_fetch_assoc($query2);
$advertiser1= mysql_num_rows($query2);
$percentadvertiser1 = (($advertiser1 / $total) * 100);
$query3 = mysql_query("select link from refererlog where link = 'advertiser2' AND datetime BETWEEN '$bdate' AND '$edate'") or die(mysql_error());
$row = mysql_fetch_assoc($query3);
$advertiser2 = mysql_num_rows($query3);
$percentadvertiser2 = (($advertiser2 / $total) * 100);
$leftover = ($total - ($advertiser1 + $advertiser2));
$percentleftover = (($leftover / $total) * 100);
[/code]
If you just want a record count it's far mor effecient to use COUNT() than to fetch the data then get the row count.

[code]$res = mysql_query("SELECT COUNT(*) FROM refererlog  WHERE datetime BETWEEN '$bdate' AND '$edate'");
$total = mysql_result ($res, 0);[/code]

For the other counts

[code]$query2 = mysql_query("select link, COUNT(*) as linkcount
     FROM refererlog
     WHERE link IN ('advertiser1', 'advertiser2')
     AND datetime BETWEEN '$bdate' AND '$edate'
     GROUP BY link");[/code]
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.