Jump to content

One big query, how to split?


sequalit

Recommended Posts

I have a query that pulls a list of recent transactions.

 

this is what gets pulled into $results from the query

 

$results =

id, date, amt, type

1 , blah, 25  , deposit

2 , blah, -10 , expense

 

My question is, I know I can create one query to get just deposits and one query to get just expenses. After i get my results into the variable $results. how can i split that variable into two sets of results, based on deposits or expenses? This way I only have to make one query and save system resources.

 

$results = mysql_query($query);

 

$expenses = hrmmm?

$deposits  = hrmmm?

Link to comment
https://forums.phpfreaks.com/topic/98575-one-big-query-how-to-split/
Share on other sites

use a join then easy as that and your sort of right...

 

but where not getting into debates what takes more resources...

<?php
$query = mysql_query("SELECT table1.deposit,table2.exspence 
FROM table1,table2 WHERE table1.type = 'deposit' AND table2.type = 'expense' " );
?>

this is my query as it stands

$sql = "".
"SELECT userID, date, transactions.name, amt, transactionTypes.transType, transactionCategories.transCat".
" FROM transactions, transactionTypes, transactionCategories".
" WHERE transactions.typeID = transactionTypes.typeID".
" AND transactions.catID = transactionCategories.catID".
" AND userID = 1".
" LIMIT 0 , 900";

 

 

now i place this in a variable called $results;

 

is there any way to run a php-based query on that variable and split it up?

 

I suppose the only way I can think of is to run the array through a while loop and add the data to each respected array...

like so:

 

$result = mysql_fetch_table($results);
$expenses = array();
$deposits = array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)){
if($row['amt'] >= 0)
              array_push($deposits, $row);
        else
              array_push($expenses, $row);
}

 

so now its only a discussion of which is faster?

 

two queries or the php parsing?

 

i'll start a new topic for this one :)

 

link to new topic:

http://www.phpfreaks.com/forums/index.php/topic,189941.0.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.