1

こんにちは皆さん、内部結合に取り組むのはこれが初めてなので、どんな助けでも大歓迎です。

作成したコードから次のエラーが発生するのはなぜだろうと思っています:

一意のテーブル/エイリアスではありません: 'products'

コード自体は次のとおりです。

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);

結合は少し難しいことがわかっていますが、助けていただければ幸いです。

4

1 に答える 1

1
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 = ...
于 2013-07-19T08:31:32.903 に答える