Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/240073-sql-php-array-result/
Share on other sites

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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