2013-07-19 73 views
0

嗨,大家好,所以這是我第一次搶斷內加入讓不勝感激任何幫助。不是唯一的表/別名:「產品」

只是想知道爲什麼我從我所創建的代碼得到以下錯誤:

不是唯一的表/別名:「產品」

這裏是代碼本身:

mysql_select_db($database_reps, $reps); 
$query_orders = sprintf("SELECT 
orders.ID AS mainID, 
customers.`Name` AS customerName, 
products.ProductName AS product, 
orders.Quantity AS Quantity, 
orders.comment As comment, 
orders.SalesPrice AS SalesPrice, 
orders.Price AS Price, 
orders.paid AS paid, 
orders.product2 AS product2, 
orders.AgeOfPayment AS AgeOfPayment, 
orders.orderDate AS orderDate, 
products.Price AS productPrice, 
staff.StaffName AS staffMember, 
orders.bonus AS bonus 
FROM 
orders 
INNER JOIN staff AS staff ON orders.staffMember = staff.ID 
INNER JOIN products AS products ON orders.product = products.ID 
INNER JOIN products AS products ON orders.product2 = products.ID 
INNER JOIN customers AS customers ON orders.customerName = customers.ID 
WHERE 
orders.ID = %s", GetSQLValueString($colname_orders, "int")); 

$orders = mysql_query($query_orders, $reps) or die(mysql_error()); 
$row_orders = mysql_fetch_assoc($orders); 
$totalRows_orders = mysql_num_rows($orders); 

聯盟證明有點困難,但任何幫助非常感謝。

回答

0
INNER JOIN products AS products ON orders.product = products.ID 
INNER JOIN products AS products ON orders.product2 = products.ID 

兩個連接到products表正在別名爲products,用於每個像products1products2不同的別名;並確保在選定列的列表中使用正確的別名;其中,雖然你的選擇列表中並沒有真正給予任何指示要引用

編輯

SELECT orders.ID AS mainID, 
     customers.`Name` AS customerName, 
     products1.ProductName AS productName1, 
     products2.ProductName AS productName2, 
     orders.Quantity AS Quantity, 
     orders.comment As comment, 
     orders.SalesPrice AS SalesPrice, 
     orders.Price AS Price, 
     orders.paid AS paid, 
     orders.product2 AS product2, 
     orders.AgeOfPayment AS AgeOfPayment, 
     orders.orderDate AS orderDate, 
     products1.Price AS productPrice1, 
     products2.Price AS productPrice2, 
     staff.StaffName AS staffMember, 
     orders.bonus AS bonus 
    FROM orders 
INNER JOIN staff 
    ON orders.staffMember = staff.ID 
INNER JOIN products AS products1 
    ON orders.product = products1.ID 
INNER JOIN products AS products2 
    ON orders.product2 = products2.ID 
INNER JOIN customers 
    ON orders.customerName = customers.ID 
WHERE orders.ID = ... 
+0

嗨,馬克,爲感謝。我只是有點好奇的佈局。你能給我一個你的意思嗎? – Rich

+0

您希望在您選擇的列列表中看到哪個產品名稱和價格?與orders.product匹配的名稱/價格,還是與orders.product2相匹配的名稱/價格?或兩者? –

+0

@ user2598646來吧,告訴我們你的第一 – Strawberry