raymonn23 Posted June 22, 2011 Share Posted June 22, 2011 I encounter a big query with 2 sub queries in my maintaining project. I want to remove this sub queries, is it possible to do something like this : function subquery(){ $sql = 'SELECT * FROM Table2'; $result = mysql_query($sql); while($row = db_fetch_array($result)){ $rows[0] = $row[0]; } return $rows; } $sql = 'SELECT Table1.*, sub1.* FROM Table1,( ' . subquery() . ' )sub1 WHERE sub1.field1 = 1 AND Table1.field1 = sub1.field1' ; $result = mysql_query($sql); ... I tried it but getting error because mysql can't read php array. If possible, code patern is really appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240073-sql-php-array-result/ Share on other sites More sharing options...
TeNDoLLA Posted June 22, 2011 Share Posted June 22, 2011 Would be probably wise to describe your table's and what kind of output you want? Quote Link to comment https://forums.phpfreaks.com/topic/240073-sql-php-array-result/#findComment-1233191 Share on other sites More sharing options...
raymonn23 Posted June 22, 2011 Author Share Posted June 22, 2011 Hi Tendolla, Thanks for the reply. Anyway my goal is to execute each sub queries individually outside the big query, so when the big query got executed, it don't need to execute the sub queries and the values are already ready.... But this post of mine make me think again.. hmm, it looks like there's no big difference even I achieve my goal because the big query will still look at the sub query data. I guess I really need to revise the query. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240073-sql-php-array-result/#findComment-1233193 Share on other sites More sharing options...
TeNDoLLA Posted June 22, 2011 Share Posted June 22, 2011 Won't really make any difference in practise if you made the queries separately or in same query with sub queries. But I just got a feeling that you might be doing it all the way wrong. Depending on what you want to fetch from db and how is your db structure build up. Can't really help much more with the information provided. If I understood, maybe use some sort of loop to run the queries and store results inside the loop? Quote Link to comment https://forums.phpfreaks.com/topic/240073-sql-php-array-result/#findComment-1233194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.