简体   繁体   中英

Mysql JOIN not returning value for joined table

I cannot for the life of me figure out why I can't get image from table B. It returns nothing from table B from everything from table A.

I'm wondering if it's table structure. The way the table is set up is there could be multiple instances of order_number in the order_detail table. Both Product_id fields are INT(10) and previously the order_detail table didn't have a primary key (auto increment). Because Order Number is taken from another table and each order could have 10 of the same order number for every product purchased, the primary key is a separate field.

Should I create the order_number as an index field? Any help would be awesome b/c I'm about to give up.

Here is order details:
id      order_number  date_time         product_id  product_name    quantity
2       10011     2012-12-20 14:11:24   13          T-Shirt         1
3       10011     2012-12-20 20:02:31   11          T-Shirt         1

Here is products:
product_id  who product_name    color   size    price   image
13          men T-shirt     red medium  15.00   /images/apparel/t-shirt.jpg
11          men T-Shirt         red small   15.00   /images/apparel/t-shirt.jpg

This is the end result of my query:

Order Number    Image   Product Name    Quantity    Cost Each   Total
10011               T-Shirt         2           $15.00          $30
10011               T-Shirt         2           $15.00      $30




$order_number = $_GET['var1'];
$query = "SELECT a.product_name, a.quantity, b.image, a.product_cost FROM order_detail a LEFT JOIN products b ON a.product_id = b.product_id WHERE a.order_number = '$order_number'";
$data = mysqli_query($dbc, $query); 

while($row = mysqli_fetch_assoc($data)){
$prod_name = $row['product_name'];
$quantity = $row['quantity'];
$cost = $row['product_cost'];
$img = $row['image'];

It works with your data and query with some changes (there is no product_cost field in order_detail table):
http://www.sqlfiddle.com/#!2/38c9b/7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM