Knowledgebase

getting data from two tables

Posted by djsmith, 12-10-2010, 11:44 AM
I am trying to find records in one table(order_detail) that are related to records in antoher table(order_master) based on the order_id. When I use: $all_orderdetails = "SELECT * from order_detail WHERE order_detail.order_id = '?'"; I get every order from my order_master table but no output from my order_detail table. If I omit the single quotes around the ? I only the the first order from the order_master table and nothing from the order_detail table

Posted by kjsrs, 12-10-2010, 12:25 PM
Try: $all_orderdetails = "SELECT * from `order_master`,`order_detail` WHERE order_master.order_id = order_detail.order_id AND order_detail.order_id = '?'";

Posted by Joseph_M, 12-10-2010, 01:45 PM
Use a JOIN to connect the two tables in your SQL statement.

Posted by centauricw, 12-11-2010, 02:07 AM
I recommend you pick up SQL Queries for Mere Mortals, which a database-engine agnostic guide to SQL queries. http://www.amazon.com/SQL-Queries-Me...2047539&sr=8-1

Posted by Mindy, 12-11-2010, 03:37 AM
use a join. look here for more info http://www.tizag.com/mysqlTutorial/mysqljoins.php

Posted by djsmith, 12-11-2010, 08:56 AM
I have researched the site listed on how to do a join in my inner while loop. My output display is giving me each of my orders from my order master table and all of my order details from the order_details table. My outer loop is working fine. with SELECT * from order_master.

Posted by qtriangle, 12-11-2010, 01:08 PM
So does it mean it solved for you?

Posted by djsmith, 12-11-2010, 03:11 PM
No, I am sure it is something simple that Iam missing or not understanding. Thank you for any insight you might have to offer.

Posted by Hostify Networks, 12-12-2010, 01:10 AM
IIRC, you must specify the columns you wish to select from rather than using the wildcard in these cases (try it with the wildcard first, of course ). Something like this should work: Last edited by Hostify Networks; 12-12-2010 at 01:13 AM.

Posted by kevin_lee, 12-13-2010, 07:21 AM
To get data from two tables, you have to mention primary key in parent table and foreign key in child table. Always use id for key.

Posted by DomainWink, 12-15-2010, 09:58 AM
Try this: This will simply output all order details, together with their corresponding orders record. If a certain order has no corresponding order detail from order_detail table, then the row for this current order won't be fetched.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Noc24 (Views: 591)
DNS Cluster (Views: 611)


Language:

Client Login

Email

Password

Remember Me

Search