If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Which one is better?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-06, 04:30
rushenas rushenas is offline
Registered User
 
Join Date: Jan 2006
Posts: 1
Exclamation Which one is better?

Hi all
I wanna know, which one of the following code is better (don't give a damn about the coding language, I just wanna know about SQL query):

Code1:

PHP Code:
$query "SELECT * FROM products, categories, options WHERE products.product_id = '".$_REQUEST["id"]."' AND categories.prod_id = products.product_id AND options.category_id = categories.category_id";
$result mysql_query($query);

for (
$i=0$i<mysql_num_rows($result); $i++)
{
     
$record mysql_fetch_array($result);
     
// Showing results ...

Code 2:

PHP Code:
$query "SELECT * FROM products WHERE products.product_id = '".$_REQUEST["id"]."'";
$result mysql_query($query);

$record mysql_fetch_array($result);
// Showing some results ...

$query "SELECT * FROM categories WHERE categories.product_id = '".$_REQUEST["id"]."'";
$result mysql_query($query);
for (
$i=0$i<mysql_num_rows($result); $i++)
{
     
$record mysql_fetch_array($result);
     
$category $record["category_id"]; 

     
$query "SELECT * FROM options WHERE options.category_id = '$category'";
     
$result2 mysql_query($query);
     for (
$j=0$j<mysql_num_rows($result2); $j++)
     {
          
$row mysql_fetch_array($result2);
          
// Show results ...
     
}

This is just a sample, of course I must have similar code for 5 tables (here is only 3 tables). So the first code will contain a more complicated query. But the second one contains more query. Which one is faster, and has better coding?

Maybe useful, products has some fields, including product_id. categories includes both product_id and category_id, and options contains option_id and category_id.
Reply With Quote
  #2 (permalink)  
Old 01-08-06, 07:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by rushenas
Which one is faster
the first one


Quote:
Originally Posted by rushenas
and has better coding?
the first one
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On