473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

mysql joins related query

pradeepjain
563 512MB
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 :(
Oct 15 '09 #1
2 1814
code green
1,726 Expert 1GB
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'?
Oct 15 '09 #2
pradeepjain
563 512MB
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) 
Oct 15 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ralph Freshour | last post by:
I am having a hard time with joins - my following code displays: ..member_name .gender instead of the actual data - I've been reading through my PHP and MySQL manuals - the MySQL manual tells...
13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
3
by: Zaphod Beeblebrox | last post by:
As much of this question relates to mysql, it may be OT? I'm trying to make a search engine for a reasonably complex database that was originally developed by someone else in Access. I've ported...
0
by: Phil Powell | last post by:
I have a rather complicated query with a combination of LEFT JOINs and two MATCHES where the first match is non-boolean to get the accurate score, the second to search as boolean: SELECT...
8
by: wlcna | last post by:
mysql v4.0.16: I had been using mysql with innodb and thought that was fine, until i used it for something requiring a few - perhaps slightly involved - joins, and have now seen the performance...
1
by: JBBHF | last post by:
Hi i'm working on a web project, and i would like to make my oracle query work in mysql. select match.numero "nummatch", to_char(match.datematch, 'yyyy-MM-dd') "datematch", p1.numjoueur "j1",...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
1
by: deepak.rao | last post by:
Hi, I have to run the following statement in MySQL. (The script is generated from oracle). ************* SELECT t1.c1, t2.c2, t3.c3 from t1, t2, t3 where t1.flag = 1 and (t1.cx = t2.cx(+)...
6
by: onnodb | last post by:
Hi all, While working on an Access UI to a MySQL database (which should be a reasonable, low-cost, flexible interface to the DB, better than web-based, much less costly than a full-fledged .NET...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.