Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Databasing
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 10-29-2009, 02:46 PM
  #1
cmetz1977
Myrmidon (Level 12)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 193
iTrader: (0)
cmetz1977 will become famous soon enough
Using a query's result in subsequent queries

I am running a script similar to the following:

PHP Code:
$customerquery mysql_query"SELECT `customerid` FROM `customers` WHERE ..."$connection );
if( 
$customerquery && mysql_num_rows$customerquery ) )
{
    
$customeridarray = array( );
    while( 
$customerresult mysql_fetch_assoc$customerquery ) )
        
$customeridarray[ ] = $customerresult'customerid' ];
    
$customeridstring implode"', '"$customeridarray );

This will create a string like : 1', '2', '3. So in a future WHERE statement I can say "WHERE `customerid` IN( '$customeridstring' )" and it will translate to "WHERE `customerid` IN( '1', '2', '3' )".

Unfortuneately this string can contain hundreds or thousands of values. Is there a way to that the initial result of the initial query and just use that in the future queries? Such as "WHERE `customerid` IN( $customerquery )"?
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-29-2009, 03:13 PM
  #2
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
would it be wrong to do
Quote:
select * from second table where customeric in (select customerid from customers where...)
So not to store the results but to repeat it as a subquery.
Should be faster.
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-29-2009, 03:28 PM
  #3
cmetz1977
Myrmidon (Level 12)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 193
iTrader: (0)
cmetz1977 will become famous soon enough
I have considered that, but i use result set about a dozen times. So I would go from the initial query, which I need to do anyways, plus 12 other queries (13 total), to the initial query plus 12 queries each containing a subquery (25 total).
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-30-2009, 12:43 AM
  #4
prasanthmj
Fighter (Level 4)
 
Join Date: Aug 2009
Posts: 34
iTrader: (0)
prasanthmj is an unknown quantity at this point
The database itself should be able to handle it using query cache
prasanthmj is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-30-2009, 10:17 AM
  #5
cmetz1977
Myrmidon (Level 12)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 193
iTrader: (0)
cmetz1977 will become famous soon enough
Thank you both for your responses...

I've run into several statements where PHP is painfully faster than MySQL when doing calculations, which is why I was hesitant of that option. Would query cache be more efficient than my $customeridstring?
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-01-2009, 05:28 PM
  #6
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
are these twelve other queries the same? You could just recycle the result from one of them...

alternatively - you could maybe create an SQL function/procedure that does what you need?

perhaps if you showed all of the queries you're running, we might be able to suggest a solution for efficientizing :p your code
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-02-2009, 09:17 AM
  #7
cmetz1977
Myrmidon (Level 12)
 
cmetz1977's Avatar
 
Join Date: May 2008
Location: Lakeland, FL
Posts: 193
iTrader: (0)
cmetz1977 will become famous soon enough
These queries are for demographics. Each query is for an individual demographic that considers all the POSTed selections that aren't itself.

For example, if there were only 3 qualities, and the user had already chosen that "gender" could be "m", and "age" could be "25" to "49", I would do the following:

PHP Code:
//Primary query
$customeridquery mysql_query"SELECT `customerid` FROM `invoices` WHERE `invoicedate` BETWEEN '...' AND '...'" );
//this is the set of 'customerids' to which all future queries are restricted

//3 demographic queries
$agequery mysql_query"SELECT `age`, count( `age` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `gender` = 'M' GROUP BY `age`" );
$genderquery mysql_query"SELECT `gender`, count( `gender` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `age` BETWEEN '25' AND '40' GROUP BY `gender`" );
$locationquery mysql_query"SELECT `location`, count( `location` ) FROM `customers` WHERE `customerid` IN ( '$customeridstring' ) AND `gender` = 'M' AND `age` BETWEEN '25' AND '40' GROUP BY `location`" );
//each query is restricted by everything but itself
//"age" is restricted by the initial $customeridquery and by the selected gender
//this way if the user wants to change their "age" values, they can see how many
//records are available for other existing "age" values within this same set of
//POSTed selections 
I have considered what I think you're implying, which is select all the demographics from one big query
PHP Code:
SELECT `age`, count( `age` ), `gender`, count( `gender` ), ... 
FROM `invoicesLEFT JOIN `customersUSING ( `customerid` ) 
WHERE `invoicedate` ... AND `age` ... AND `gender` ... 
However I could not figure out how to properly group this since the demographics are independant of each other. Also, the result set would be restricted to already selected values. I want the user to see what the selection would look like before they added 'F' to their 'gender' selection without having to add it to see what changed. With this query they would only get a count of 'M'.
__________________
Never accept Kool-aid from someone who won't drink from the same punch bowl.
cmetz1977 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 02:26 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.