Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-26-2005, 03:33 AM
CommuneZoom CommuneZoom is offline
 
Join Date: Sep 2005
Posts: 29
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Simplying a few MySQL Queries - Help Needed

I have the following queries that, if possible, I need to simplify. I'd like to knock this down to as few queries as possible as I will need to call additional (probably 2 other items) from the same row.

I am just looking to get the number of rows matching each set, though I'd rather knock 4 queries down to 1-2 if there is another way to do so.

PHP Code:
$getpending $db->query_read("SELECT * FROM son_customers WHERE ispending = '1'");
$totalpending $db->num_rows($getpending);

$getactive $db->query_read("SELECT * FROM son_customers WHERE ispending = '0'");
$totalactive $db->num_rows($getactive); 
Is there an alternative, of will I be stuck making 4 seperate calls?
Reply With Quote
  #2  
Old 10-26-2005, 03:48 AM
Guest190829
Guest
 
Posts: n/a
Default

Try the below, maybe it will work. I need to start coding php more, and stop with vb.net. I'm starting to forget stuff!

PHP Code:
 $getcustomers $db->query_read("SELECT COUNT(*) FROM son_customers");
 WHILE (
$result $db->fetch_array($getcustomers))
{
    if (
$result[ispending] == '0')
    {
        
$totalpending $totalpending 1
    
}
    else
    {
        
$totalactive $totalactive 1
    
}

Reply With Quote
  #3  
Old 10-26-2005, 04:09 AM
CommuneZoom CommuneZoom is offline
 
Join Date: Sep 2005
Posts: 29
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Danny.VBT
Try the below, maybe it will work. I need to start coding php more, and stop with vb.net. I'm starting to forget stuff!

PHP Code:
 $getcustomers $db->query_read("SELECT COUNT(*) FROM son_customers");
 WHILE (
$result $db->fetch_array($getcustomers))
{
    if (
$result[ispending] == '0')
    {
        
$totalpending $totalpending 1
    
}
    else
    {
        
$totalactive $totalactive 1
    
}

Thanks for the reply, however, I need them both to be showing at the same time and not alternating.

As I need it to report the total for the entire table, the above will not work. I have 8 entries in the table and the above reports a blank for active and 1 for pending (when all 8 are pending).
Reply With Quote
  #4  
Old 10-26-2005, 04:12 AM
Guest190829
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by CommuneZoom
Thanks for the reply, however, I need them both to be showing at the same time and not alternating.

As I need it to report the total for the entire table, the above will not work. I have 8 entries in the table and the above reports a blank for active and 1 for pending (when all 8 are pending).
Try it without the Count clause. So...

PHP Code:
 $getcustomers $db->query_read("SELECT * FROM son_customers");
 WHILE (
$result $db->fetch_array($getcustomers))
{
    if (
$result[ispending] == '0')
    {
        
$totalpending $totalpending 1
    
}
    else
    {
        
$totalactive $totalactive 1
    
}

Reply With Quote
  #5  
Old 10-26-2005, 06:02 AM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I think the logic may be backwards in that, totalactive should be when ispending = 0.

Try ;

PHP Code:
$getcustomers $db->query_read("SELECT ispending FROM son_customers"); 
 WHILE (
$result $db->fetch_array($getcustomers)) 

    if (
$result[ispending]) 
    { 
        
$totalpending += 
    

    else 
    { 
        
$totalactive += 
    


Reply With Quote
  #6  
Old 10-26-2005, 12:43 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$getcustomers $db->query_first("SELECT SUM(IF(ispending = 1, 1, 0)) as pending,  SUM(IF(ispending = 0, 1, 0)) as active FROM son_customers"); 
finished.
Reply With Quote
  #7  
Old 10-26-2005, 04:53 PM
Guest190829
Guest
 
Posts: n/a
Default

Can you explain this bit?

[sql]IF(ispending = 1, 1, 0)) [/sql]

I've never seen this before...
Reply With Quote
  #8  
Old 10-26-2005, 05:43 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

if the value of ispending is '1', it will result in a value of 1, otherwise 0 (zero), if you then sum it, you will count 1 for each row that have ispending = 1.

By testing ispending = 0, you can reverse the count.
Reply With Quote
  #9  
Old 10-26-2005, 05:50 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarcoH64
PHP Code:
$getcustomers $db->query_first("SELECT SUM(IF(ispending = 1, 1, 0)) as pending,  SUM(IF(ispending = 0, 1, 0)) as active FROM son_customers"); 
finished.
Interesting question, is it quicker to use a more complicated SQL query, or a simpler query and a bit of PHP.
Reply With Quote
  #10  
Old 10-26-2005, 08:48 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That depends totally on the query. Since this one are simple embedded functions, not used in where/order by/join conditions, i would say the query is much faster then doing it in php.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:48 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04076 seconds
  • Memory Usage 2,269KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (7)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (7)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete