Connecting Tech Pros Worldwide Help | Site Map

mysql joins related query

pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 406
#1: Oct 15 '09
I have 2 tables like

Expand|Select|Wrap|Line Numbers
  1. desc product_subcategory;
  2. +----------------+--------------+------+-----+---------+----------------+
  3. | Field          | Type         | Null | Key | Default | Extra          |
  4. +----------------+--------------+------+-----+---------+----------------+
  5. | product_id     | int(11)      | NO   | PRI | NULL    | auto_increment | 
  6. | product_name   | varchar(200) | NO   |     | NULL    |                | 
  7. | subcategory_id | int(11)      | NO   | MUL | NULL    |                | 
  8. +----------------+--------------+------+-----+---------+----------------+
  9.  
  10. desc product_features;
  11. +--------------+--------------------------------------------------------------------------------------+------+-----+---------+----------------+
  12. | Field        | Type                                                                                 | Null | Key | Default | Extra          |
  13. +--------------+--------------------------------------------------------------------------------------+------+-----+---------+----------------+
  14. | id           | int(11)                                                                              | NO   | PRI | NULL    | auto_increment | 
  15. | product_id   | int(11)                                                                              | NO   | MUL | NULL    |                | 
  16. | type         | enum('CDMA','GSM','BOTH')                                                            | YES  |     | NULL    |                | 
  17. | Price        | enum('5000-10000','10000-15000','15000-20000','20000-25000','25000-30000')           | YES  |     | NULL    |                | 
  18. | main_feature | enum('BUSINESS PHONE','SMART PHONE','BASIC PHONE','MULTIMEDIA PHONE','CAMERA PHONE') | YES  |     | NULL    |                | 
  19. | weight       | enum('100-200','200-400','400-800','800-1600')                                       | YES  |     | NULL    |                | 
  20. | pic          | longblob                                                                             | YES  |     | NULL    |                | 
  21. +--------------+--------------------------------------------------------------------------------------+------+-----+---------+----------------+
  22. 7 rows in set (0.00 sec)

i need to query 2 tables to get the data out was wondering if there is a way to use joins and do it in a single query !!

here is ma php code
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql="select * from product_subcategory where subcategory_id='$sub_category'";
  3. $result=mysql_query($sql);
  4. $count = mysql_num_rows($result);
  5. $myresult ='';
  6. $myresult .="<div id='change'>";
  7. $myresult .="<table border='1' width='100%'>";
  8. $myresult .="<tr><td><b>Pic</b></td><td><b>Type</b></td><td><b>Price</b></td><td><b>Feature</b></td><td><b>Weight</b></td></tr>";
  9. $myresult .="<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
  10. if($count > 0){
  11. while($row =  mysql_fetch_array($result)){
  12. $new = $row['product_id'];
  13. $sql1="select * from product_features where product_id='$new'";
  14. $result1=mysql_query($sql1);
  15. while($row1=mysql_fetch_array($result1)){
  16. $myresult .="<tr>";
  17. $myresult .= "<td><center><img src='picdisplay.php?id=". $row1['product_id'] . "'</img></center><br/><center>".$row['product_name']. "</center></td>";
  18. $myresult .= "<td>" . $row1['type']. "</td>";
  19. $myresult .= "<td>" . $row1['Price']. "</td>";
  20. $myresult .= "<td>" . $row1['main_feature']. "</td>";
  21. $myresult .= "<td>" . $row1['weight']. "</td>";
  22. $myresult .="</tr>";
  23. }
  24. }
  25. }

can i do thing in a single query using joins!!!

i tried something like this

Expand|Select|Wrap|Line Numbers
  1. $sql1="select * from product_features join product_subcategory on product_features.product_id=product_subcategory.product_id and product_subcategory.subcategory_id='$sub_category'";

but did not work :(
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#2: Oct 15 '09

re: mysql joins related query


Expand|Select|Wrap|Line Numbers
  1. $sql1="select * from product_features 
  2. join product_subcategory on
  3. product_features.product_id=product_subcategory.product_id 
  4. and product_subcategory.subcategory_id='$sub_category'"; 
  5.  
It looks fine to me.
I think in this case I would prefer to write
Expand|Select|Wrap|Line Numbers
  1. $sql1="select * from product_subcategory 
  2. LEFT join product_features USING (product_id) 
  3. WHERE product_subcategory.subcategory_id='$sub_category'"; 
  4.  
But this should yield the same result.
What do you mean by 'Doesn't work'?
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 406
#3: Oct 15 '09

re: mysql joins related query


sorry i did not get back! that worked for me ! one loop was extra so it was repeating the entries ..

hope i am correct on this
instead of this
Expand|Select|Wrap|Line Numbers
  1. on product_features.product_id=product_subcategory.product_id 
you are using

Expand|Select|Wrap|Line Numbers
  1. USING (product_id) 
Reply