gpt4 book ai didi

mySQL 内部连接多个表

转载 作者:行者123 更新时间:2023-12-01 00:38:38 25 4
gpt4 key购买 nike

如果我发布这行代码:

SELECT Cheese.Chesse_Name
FROM Customer
INNER JOIN Customer ON Customer.Customer_ID = Orders.Customer_ID
INNER JOIN Orders ON Orders.Orders_ID = OrderSandwich.Orders_ID
INNER JOIN OrderSandwich ON OrderSandwich.Sandwich_ID = Sandwich.Sandwich_ID
INNER JOIN Sandwich ON Sandwich.Cheese_ID = Cheese.Cheese_ID
WHERE Customer.CustomerID = 1;

我从数据库中得到的只是一个错误

Fehler

SQL-Befehl: Dokumentation

SELECT Cheese.Chesse_Name
FROM Customer
INNER JOIN Customer ON Customer.Customer_ID = Orders.Customer_ID
INNER JOIN Orders ON Orders.Orders_ID = OrderSandwich.Orders_ID
INNER JOIN OrderSandwich ON OrderSandwich.Sandwich_ID = Sandwich.Sandwich_ID
INNER JOIN Sandwich ON Sandwich.Cheese_ID = Cheese.Cheese_ID
WHERE Customer.CustomerID = 1
LIMIT 0, 25

MySQL meldet: Dokumentation
#1066 - Not unique table/alias: 'Customer'

我现在正在尝试这个几个小时而不是说空洞日。

这是数据库图:

enter image description here

如果有人马上看到我犯的错误,我将非常感激

最佳答案

您正在尝试将 Customer 表与其自身连接起来。重新组织您的 JOIN 查询,如下所示。

SELECT Cheese.Cheese_Name
FROM Customer
INNER JOIN Orders ON Customer.Customer_ID = Orders.Customer_ID
INNER JOIN OrderSandwich ON Orders.Orders_ID = OrderSandwich.Orders_ID
INNER JOIN Sandwich ON OrderSandwich.Sandwich_ID = Sandwich.Sandwich_ID
INNER JOIN Cheese ON Sandwich.Cheese_ID = Cheese.Cheese_ID
WHERE Customer.Customer_ID = 1;

请记住,您正在调用 JOIN 语句,将提到的表与上面的表连接起来。例如,如果您调用

Customer
INNER JOIN Orders ON Customer.Customer_ID = Orders.Customer_ID

您正在加入 Customer 表和 Orders 表。无需两次提及 Customer 表。

关于mySQL 内部连接多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40348043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com