0

我尝试根据第二个表的寄存器从一个表中进行选择

我使用 4 张桌子

table Products
Product_Id     |  Product_Name    |   Product_Value   | Category_Id
1              |  Ball            |   10              | 1
2              |  Pen             |    2              | 2 
3              |  Eraser          |    1              | 1

table Categories
Category_Id    |  Category_Name
1              |  Acessories
2              |  Others

table Customers
Customer_Id    |  Customer_Name
1              |  John
2              |  Peter

table Sells
Sell_Id        |  Product_Id      |   Customer_Id   |  Sell_Date
1              |  1               |   1             |  2012-01-01
2              |  2               |   1             |  2012-01-02

所以我想做出一个选择来展示客户历史上他买的东西

约翰历史

Product Name  Category Name   Date
Ball          Accessories     2012-01-01
Pen           Others          2012-010

在最后我显示信息表格 3 个不同的表格

我尝试使用 INNER JOIN 和 LEFT JOIN 但没有成功。

谢谢你的帮助。

4

5 回答 5

5

分解:您想要的第一件事是产品名称:

SELECT Product_Name FROM Products

我们如何获得类别?加入吧!

SELECT Products.Product_Name, Categories.Category_Name 
FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id

越来越接近!是时候引入订单历史了:

SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date 
FROM Products 
JOIN Categories ON Products.Category_Id = Categories.Category_Id
JOIN Sells ON Sells.Sells_Id = Products.Product_Id

现在我们可以引入名称,并过滤到 John:

SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date 
FROM Products 
JOIN Categories ON Products.Category_Id = Categories.Category_Id
JOIN Sells ON Sells.Sells_Id = Products.Product_Id
JOIN Customers ON Sells.Customer_Id = Customers.Customer_Id
WHERE Customers.Customer_Name = 'John'

在你的脑海中一步一步地走。随时从每个部分构建查询。

既然你提到了...

LEFT JOIN是你如何处理丢失的数据。它从“左”表返回行,如果子句NULL不匹配,则放入“右”表的字段。ON

例如,如果 John 购买了没有类别 ( Product.Category_Idwas NULL) 的产品,则使用 aJOIN不会返回该购买记录,因为没有与 匹配的类别Category_Id Null。在这种情况下,您可以LEFT JOIN Categories并且在结果集中Category_Name也可以NULL

于 2012-11-21T04:22:53.587 回答
2
SELECT
    Customers.Customer_Name as 'Customer Name',
    Products.Product_Name as 'Product Name',
    Categories.Category_Name as 'Category Name',
    Sells.Sell_Date as 'Date Sold'
FROM Customers
    INNER JOIN Sells on Sells.Customer_Id = Customers.Customer_Id
    INNER JOIN Products ON Products.Product_Id = Sells.Sell_Id
    INNER JOIN Categories ON Categories.Category_Id = Products.Category_ID;

如果要为一个人提取历史记录,可以使用 WHERE 子句:

WHERE Customers.Customer_Id = 1

或者...

WHERE Customers.Customer_Name = 'John'
于 2012-11-21T04:22:37.630 回答
1

只需在所有这些表上使用 JOIN 并选择您想要的任何内容:

SELECT 
    p.Product_Name, c.Category_Name, s.Sell_Date
FROM
    Customers cu JOIN Sells s ON cu.id = s.Customer_Id
    JOIN Products p ON p.id = s.Product_Id     
    JOIN Categories c ON p.Category_Id = c.id
WHERE
    cu.Customer_Name = "John"
于 2012-11-21T04:19:32.333 回答
1

JOIN是正确的:

SELECT Products.Product_Name, Categories.Category_Name, Sells.Sell_Date
FROM Sells JOIN Products ON Sells.Product_Id = Products.Product_Id JOIN Categories ON Sells.Category_Id = Categories.Category_Id
WHERE Sells.Customer_Id = 1;
于 2012-11-21T04:21:49.190 回答
0
SELECT Product_Name, Category_Name, Sell_Date
FROM Products, Categories, Sells
WHERE Sells.Customer_Id = 1
    AND Sells.Product_Id = Products.Product_Id
    AND Products.Category_Id = Categories.Category_Id;
于 2012-11-21T04:22:07.613 回答