sequalit Posted March 30, 2008 Share Posted March 30, 2008 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 More sharing options...
Northern Flame Posted March 30, 2008 Share Posted March 30, 2008 you can just use the WHERE statement instead, so like this: $expenses_query = mysql_query("SELECT * FROM table WHERE type = 'expense'"); $deposit_query = mysql_query("SELECT * FROM table WHERE type = 'deposit'"); and then echo out the data Link to comment https://forums.phpfreaks.com/topic/98575-one-big-query-how-to-split/#findComment-504513 Share on other sites More sharing options...
sequalit Posted March 30, 2008 Author Share Posted March 30, 2008 I was trying to keep from doing two separate queries. to save system resources (I'm running a Virtual Private Server and have limited RAM) As far as I know, MySQL queries take a lot more system resources than doing something in PHP correct me if Im wrong. Link to comment https://forums.phpfreaks.com/topic/98575-one-big-query-how-to-split/#findComment-504515 Share on other sites More sharing options...
redarrow Posted March 30, 2008 Share Posted March 30, 2008 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' " ); ?> Link to comment https://forums.phpfreaks.com/topic/98575-one-big-query-how-to-split/#findComment-504533 Share on other sites More sharing options...
sequalit Posted March 30, 2008 Author Share Posted March 30, 2008 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 Link to comment https://forums.phpfreaks.com/topic/98575-one-big-query-how-to-split/#findComment-504544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.