RE: Looking for a little MySQL help as well

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Phillip Baker [mailto:phillip@xxxxxxxxxxxx] 
Sent: Monday, July 12, 2010 1:33 PM
To: Tommy Pham
Cc: PHP General List
Subject: Re:  Looking for a little MySQL help as well

Thanks Tommy,

Damn.
I was hoping to avoid that and get all the information in one query rather
than running a ton of queries. :-(
But might be why I am having trouble finding an answer for this on the net.

Blessed Be

Phillip



Phillip,

I think you misunderstood.  Your query is still 1 query.  You just loop
through the results and compare certain field for changes using 'if' to get
the desired effect for your sample tables:

Table 1
Product_id  |  Product_Name
1                |  Product A
2                |  Product B
3                |  Product C

Table 2
Category_id  |  Category_Name
1                 |   Admin
2                 |   Marketing
3                 |   Support
4                 |   IT


Table 3
Product_id  |  Category_id
1                |  1
1                |  3
2                |  2
3                |  3
3                |  4

Result would look like
Product A, Admin, Support
Product B, Marketing
Product C, Support, IT



Here's the pseudo code:

$last_product_id = null;
$current_product_id = null;
Loop $result_set if not end of $result_set
  $current_product_id = get("Product_id") from $result_set;
  If ($last_product_id != $current_product_id)
  {
     $last_product_id = $current_product_id;
     $product_name = get("Product_Name") from  $result_set;
     $category_name = get("Category_Name") from $result_set;
   }
   Else
  {
     $category_name .= ', '. get("Category_Name") from $result_set;
    }

//  do as you will with $product_name & $category_name

Next loop iteration

IMO, this is way faster to implement than trying to figure out all that SP
stuff since we're not DBAs ;) .  As for overall performance, I don't know if
this is faster than SP.  You'll just have to analyze your data size and what
kind of traffic you're getting vs. hardware(s) you have to see if it's worth
the time to learn how to write SP.  Just an FYI, once you get into complex
situations, SP have higher security and generally have better performance
than just standard queries.

Regards,
Tommy


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux