php - 不唯一的表/别名 : 'products'

标签 php mysql inner-join

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

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

不唯一的表/别名:“产品”

这是代码本身:

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

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

最佳答案

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 = ...

关于php - 不唯一的表/别名 : 'products' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741471/

相关文章:

mysql - 将 2 个内连接查询合并为一个

mysql - 多表Mysql JOIN

php - 如何在 Laravel 4 中使用参数和 CASE 语句进行更新查询

sql - 如何在 MySQL 中强制执行复合唯一性?

mysql - 数据库设计 - 困惑

PHP MySQL - 通知面板,单击下拉菜单时更新数据库

mysql - 选择表 A 中与表 B 中的一组项目相连接的行

javascript - 为什么我不断收到 undefined variable : errorEmpty and errorEmail in my script tags?

php - 在 TWIG 中渲染多维数组

php - Mysqli query Injection ,如何注入(inject)SQL查询字符串?