vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   MySQL query help please (https://vborg.vbsupport.ru/showthread.php?t=47286)

Darth Cow 01-02-2003 07:38 PM

MySQL query help please
 
Here is my current MySQL query...

SELECT SUM(hitscount) AS totalhits FROM hits WHERE time>123456789 AND siteid=10 GROUP BY siteid

This query works great most of the time, but sometimes there won't be any result rows that match the criteria. How would I make totalhits be equal to 0 if there are no results returned instead of a mysql resource result with no rows? Thanks for you help :).

Xenon 01-02-2003 07:51 PM

this part here is useless if it's the full query:

AND siteid=10 GROUP BY siteid

just remove the group by and i think it should work...

Darth Cow 01-02-2003 11:45 PM

I realized that I was needlessly added in the SUM() part that was already taken care of by the grouping, so it was repetative. The solution I can up with on my own (using mysql_num_rows()) isn't nearly as elegant as merely removing the group by part and using intval() on the result. Thanks for you help :).

TECK 01-03-2003 12:24 AM

Quote:

Originally posted by Darth Cow
Here is my current MySQL query...

SELECT SUM(hitscount) AS totalhits FROM hits WHERE time>123456789 AND siteid=10 GROUP BY siteid

This query works great most of the time, but sometimes there won't be any result rows that match the criteria. How would I make totalhits be equal to 0 if there are no results returned instead of a mysql resource result with no rows? Thanks for you help :).

PHP Code:

$count['hits'] = 0;
if ( 
$docount $DB_site->query_first("
  SELECT COUNT(*) AS totalhits
  FROM hits
  WHERE dateline>
$bbuserinfo[lastvisit] AND siteid=10
"
) and !empty( $docount['totalhits'] ) )
{
  
$count['hits'] = number_format$docount['totalhits'] );



Logik 01-03-2003 12:32 AM

I think i see a typo in that..

Logik 01-03-2003 12:33 AM

PHP Code:

$count['hits'] = 0;
if ( 
$docount $DB_site->query_first)"
  SELECT COUNT(*) AS totalhits
  FROM hits
  WHERE dateline>
$bbuserinfo[lastvisit] AND siteid=10
"
) and !empty( $docount['totalhits'] ) )
{
  
$count['hits'] = number_format$docount['totalhits'] );


You had the ')' turned the wrong way.. I could be wrong.. :paranoid:

TECK 01-03-2003 12:35 AM

The code above will give you a very nice error. My code is correct.

Logik 01-03-2003 12:41 AM

Yes. Indeed. A VERY nice error.. Hey, cant hate me cause im trying ;)

TECK 01-03-2003 12:47 AM

That's how we all learn, by doing mistakes. :)
Cheers.

Logik 01-03-2003 01:45 AM

hehe... :OI I just seen that '(' and was thinking.. 'whoa dont it have to be the other way?'.. But i was wrong :(

Darth Cow 01-03-2003 03:25 AM

Thanks for the code, TECK, but I already had figured out a solution :). Your code wouldn't work anyways though, besides the fact that I'm not doing in vB, that also doesn't fit my data format :D. Here is what I ended up doing...

PHP Code:

$hits_query mysql_query("SELECT SUM(hitscount) AS totalhits FROM hits
WHERE time>" 
. (time() - 60*60*24*3) . " AND siteid={$site['siteid']}");
$site['hits'] = intval(mysql_result($hits_query0)); 


TECK 01-03-2003 02:31 PM

No need to do a SUM since you only want to count the total.
Code:

$ourtime = time() - 259200;
$hits_query = mysql_query("
  SELECT COUNT(*) AS totalhits
  FROM hits
  WHERE time>$ourtime AND siteid=$site[siteid]
");

Just to make sure is ANSI compatible...

Darth Cow 01-03-2003 07:24 PM

Actually, I do, because my database is structured so that it updates hits in a batch when an updating script parses a much less stressful flat file that saves sites visited. So "hitscount" is the number of hits to that site within the 15 minutes between the cron job running :).

TECK 01-04-2003 04:54 PM

True, it makes sense if you have it set this (better) way. ;)


All times are GMT. The time now is 04:43 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01041 seconds
  • Memory Usage 1,750KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (14)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete