簡體   English   中英

Mysql JOIN 不返回連接表的值

[英]Mysql JOIN not returning value for joined table

我一生都無法弄清楚為什么我無法從表 B 中獲取圖像。它從表 A 的所有內容中都沒有從表 B 返回任何內容。

我想知道它是否是表結構。 該表的設置方式是 order_detail 表中可能有多個 order_number 實例。 兩個 Product_id 字段都是 INT(10) 並且之前 order_detail 表沒有主鍵(自動增量)。 因為訂單號是從另一個表中獲取的,並且每個訂單對於購買的每個產品可以有 10 個相同的訂單號,所以主鍵是一個單獨的字段。

我應該創建 order_number 作為索引字段嗎? 任何幫助都會很棒 b/c 我即將放棄。

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'];

它適用於您的數據並進行一些更改(order_detail 表中沒有 product_cost 字段):
http://www.sqlfiddle.com/#!2/38c9b/7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM