【问题标题】:Not unique table/alias: 'products'不是唯一的表/别名:“产品”
【发布时间】:2013-07-19 08:30:04
【问题描述】:

大家好,这是我第一次处理内部连接,非常感谢任何帮助。

只是想知道为什么我从我创建的代码中收到以下错误:

不是唯一的表/别名:'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);

事实证明,加入有点困难,但非常感谢任何帮助。

【问题讨论】:

    标签: php mysql inner-join


    【解决方案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 = ...
    

    【讨论】:

    • 嗨,马克,谢谢。我只是对布局有点好奇。你能举例说明你的意思吗?
    • 您希望在所选列列表中看到哪个产品名称和价格?与orders.product 匹配的名称/价格,还是与orders.product2 匹配的名称/价格?还是两者兼而有之?
    • @ Strawberry 我已将其更改为以下内容: INNER JOIN products AS products ON orders.product = products.ID INNER JOIN products AS products2 ON orders.product2 = products.ID 没有错误回来,但我例如: $product = $row_orders['product']; $product2 = $row_orders['product2'];当我回显 product2 时,我什么也得不到
    • @MarkBaker 嗨,马克,我输入了那个编辑,但是当我回显 $product2 时,我仍然只返回它的 ID。我有这样的列表:$product2 = $row_orders['product2'];
    • 是的,product2 将向您显示 id,因为它是 orders.product2 值...您需要查看 $row_orders['productName1'] 和 $row_orders['productName2'] 和$row_orders['productPrice1'] 和 $row_orders['productPrice2']
    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多