简体   繁体   English

MySQL多个内部联接?

[英]MySQL multiple inner joins?

I have three tables... 我有三张桌子...

create table customers (
customerID integer unsigned not null auto_increment,
customername varchar(45) not null,
address varchar(45) not null,
city varchar(45) not null,
state varchar(2) not null,
zip mediumint(5) zerofill not null,
primary key (customerID)
);

create table products (
prodID integer unsigned not null auto_increment,
prodname varchar(45) not null,
prodcat integer unsigned,
proddesc varchar(75) not null,
price float (8,2),
qoh integer not null,
prodiconurl varchar(45),
primary key (prodID),
constraint FK_CAT foreign key FK_CAT (prodcat)
    references categories (catID)
    on delete restrict
    on update restrict
);

create table SHOPPING_CART (
CustomerID INTEGER UNSIGNED NOT NULL,
ProductID INTEGER UNSIGNED NOT NULL,
ProdQnty INTEGER UNSIGNED NOT NULL,
Primary Key (CustomerID, ProductID),
CONSTRAINT Fk_Cust Foreign Key Fk_Cust (CustomerID)
    References Customers (CustID)
    On Delete restrict
    On Update restrict, 
CONSTRAINT Fk_Prod Foreign Key Fk_Prod (ProductID)
    References Products (ProdID)
    On Delete restrict
    On Update restrict
);

The tables are fine. 桌子很好。 what I'm having problems with is my SELECT statement for SHOPPING_CART. 我遇到的是我对SHOPPING_CART的SELECT语句。 If I insert the following code. 如果我插入以下代码。

insert into SHOPPING_CART
Values (
1, 4, 2
);
insert into SHOPPING_CART
values (
1, 9, 1
);

My inner join does not work for looking up the CustID, returning the FirstName and LastName fields from the Customers tables and returning the Product name from the Products table, based on values in the shopping cart table. 基于购物车表中的值,我的内部联接不适用于查找CustID,从“客户”表返回“名字”和“姓氏”字段以及从“产品”表返回“产品”名称。

Here's my ultimate goal. 这是我的最终目标。

SELECT 
Customers.FirstName,
Customers.LastName,
Products.Name,
SHOPPING_CART.ProdQnty
FROM SHOPPING_CART
inner join Customers on SHOPPING_CART.CustomerID = Customers.CustID
where Customers.CustID = SHOPPING_CART.CustomerID
inner join Products on SHOPPING_CART.ProductID = Products.ProdID
where Products.ProdID = SHOPPING_CART.ProductID;

I get a statement error when the second inner join is part of the statement, also the statement has a red x on the second inner join. 当第二个内部联接是该语句的一部分时,我收到一条语句错误,并且该语句在第二个内部联接上也带有红色的x。

Only 1 where statement is required. 仅1个需要声明的地方。 The second where statement would go underneath an "AND" statement. 第二个where语句将位于“ AND”语句的下面。 Note: The code is pre ANSI-92. 注意:该代码是ANSI-92之前的版本。 Explanation of how to use ANSI-92 follows code example. 下面的代码示例说明了如何使用ANSI-92。

SELECT 
Customers.FirstName,
Customers.LastName,
Products.Name,
SHOPPING_CART.ProdQnty
FROM SHOPPING_CART
WHERE Customers.CustID = SHOPPING_CART.CustomerID
AND Products.ProdID = SHOPPING_CART.ProductID;

History: 历史:

Prior to ANSI 92, joins within the where statements were common. 在ANSI 92之前,where语句中的联接很常见。 After the standardization, the joins moved from the where statement to the from statement. 标准化之后,联接从where语句移动到from语句。 Where statements are used to provide additional filtering capability eg where status = 'open'. where语句用于提供其他过滤功能,例如,其中status ='open'。 The following excerpt explains ANSI-92 join syntax. 以下摘录说明了ANSI-92连接语法。

The ANSI Join Syntax Before the ANSI SQL-92 standard introduced the new join syntax, relations (tables, views, etc.) were named in the FROM clause, separated by commas. ANSI连接语法在ANSI SQL-92标准引入新的连接语法之前,关系(表,视图等)在FROM子句中命名,并用逗号分隔。 Join conditions were specified in the WHERE clause: 连接条件在WHERE子句中指定:

=> SELECT * FROM T1, T2 WHERE T1.id = T2.id; =>选择*从T1,T2在T1.id = T2.id; The ANSI SQL-92 standard provided more specific join syntax, with join conditions named in the ON clause: ANSI SQL-92标准提供了更特定的连接语法,并在ON子句中指定了连接条件:

=> SELECT * FROM T1 [ INNER | =>选择*从T1 [内部| LEFT OUTER | 左外| RIGHT OUTER | 右外| FULL OUTER | 外衣| NATURAL | 自然| CROSS ] JOIN T2 ON T1.id = T2.id See SQL-99 ANSI syntax at BNF Grammar for SQL-99 for additional details. C2交叉T1.id = T2.id请参见BNF语法中有关SQL-99的SQL-99 ANSI语法,以获取更多详细信息。

Advantages: 好处:

SQL-92 outer join syntax is portable across databases; SQL-92外部联接语法可跨数据库移植; the older syntax was not consistent between databases. 数据库之间的旧语法不一致。 SQL-92 syntax provides greater control over whether predicates are to be evaluated during or after outer joins. SQL-92语法提供了对在外部联接期间或之后评估谓词的更好控制。 This was also not consistent between databases when using the older syntax. 使用较旧的语法时,这在数据库之间也不一致。 See "Join Conditions vs. Filter Conditions" below. 请参阅下面的“加入条件与过滤条件”。 SQL-92 syntax eliminates ambiguity in the order of evaluating the joins, in cases where more than two tables are joined with outer joins. 如果使用外部联接对两个以上的表进行联接,则SQL-92语法消除了评估联接顺序的歧义。 Union joins can be expressed using the SQL-92 syntax, but not in the older syntax. 联合联接可以使用SQL-92语法表示,但不能使用较旧的语法表示。

Updated Query incorporating ANSI-92: 更新了包含ANSI-92的查询:

SELECT 
Customers.FirstName,
Customers.LastName,
Products.Name,
SHOPPING_CART.ProdQnty
FROM SHOPPING_CART
inner join Customers on SHOPPING_CART.CustomerID = Customers.CustID
inner join Products on SHOPPING_CART.ProductID = Products.ProdID

If you use JOIN then you not need where. 如果使用JOIN,则不需要在哪里。

Your request will be 您的要求将是

SELECT 
  Customers.FirstName,
  Customers.LastName,
  Products.Name,
  SHOPPING_CART.ProdQnty
FROM SHOPPING_CART
INNER JOIN Customers ON SHOPPING_CART.CustomerID = Customers.CustomerID
INNER JOIN Products on SHOPPING_CART.ProductID = Products.ProdID

And I found one error in on clause in your code. 而且我在代码中的on子句中发现了一个错误。 You used CustID for table Customers. 您将CustID用于表“客户”。 But in your create table this fields is CustomerID. 但是在您的创建表中,该字段是CustomerID。 Don't know where is mistake - just wrote request for your structure. 不知道哪里出了错-只是为您的结构写了请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM