为什么这些连接查询返回重复记录?

时间:2015-11-08 06:09:41

标签: java mysql core

表格结构:

    Field                Type           Null        Default
    --------------------------------------------------------------------
    OrderId               bigint(20)     Yes        II is a Primary key 
    CustomerID            bigint(20)     Yes        
    OrderDate             date           Yes        
    ShippedDate           date           Yes        NULL
    Freight               int(20)        Yes        NULL
    ShipName              varchar(50)    Yes        NULL
    ShipAddress           varchar(100)   Yes        NULL
    ShipCity              varchar(50)    Yes        NULL
    ShipPostalCode        int(20)        Yes        NULL
    ShipCountry           varchar(30)    Yes        NULL
    ShipVia               int(50)        Yes        1
    customerPayA          int(11)        Yes         
    discount              int(11)        Yes        
    shippingPackagingCost int(11)        Yes     
    grandTotal            int(11)        Yes        NULL
    remainPayment         int(20)        Yes        NULL
    Table structure for table orderdetails
    Field            Type            Null        Default
    ProductId        int(20)        Yes        
    OrderId          int(20)        Yes        
    UnitPrice        int(200)       Yes        
    Qty              int(20)        Yes        
    vat              int(11)        Yes        
    Amount           int(20)        Yes        
    Table structure for table products
    Field        Type        Null        Default
    ProductId     int(20)        Yes        
    ProductName   varchar(50)    Yes        
    Table structure for table shippers
    Field        Type        Null        Default
    ShipperID    int(11)       Yes        
    companyName  varchar(30)   Yes            
    Table structure for table customers
    Field               Type        Null        Default
    CustomerID        int(20)            Yes        
    firstName         varchar(20)        Yes        
    middleName        varchar(20)        Yes        
    LastName          varchar(20)        Yes        
    address           varchar(200)       Yes        
    city              varchar(20)        Yes        
    postalcode        int(10)            Yes        
    country           varchar(20)        Yes  

选择查询:

SELECT DISTINCT
    o.OrderID, o.CustomerID, 
    c.firstName as BillName , 
    s.CompanyName as ShipperName,  
    p.ProductName, od.UnitPrice, od.Qty, od.Amount,  
    o.grandTotal 
FROM 
    Orders o  
JOIN 
    Customers c ON o.CustomerID = c.CustomerID  
JOIN  
    Shippers s ON o.ShipVia = s.ShipperID  
JOIN 
    OrderDetails od ON o.OrderID = od.OrderID  
JOIN 
    Products p ON od.ProductID = p.ProductID  
WHERE 
    o.OrderID =46 
  1. orderid下的订单表是主键
  2. customerid下的customer表是主键
  3. 托运人表下的托运人表是主键
  4. [![带有输出的sql选择查询下的图像有助于理解] [1]] [1]

    输出如下:

    OrderID CustomerID         BillName Shipper Product UnitPrice         Qty Amount grandTotal
    
        46                  1              bharat         balaji         evergreent         400         4         1600         3885
        46                  1              bharat         balaji         evergreent         400         4         1600         3885
        46                  1              bharat         balaji         corogen         700         3         2100         3885
        46                  1              bharat         balaji         corogen         700         3         2100         3885
    

3 个答案:

答案 0 :(得分:0)

您正在寻找一个小组:

您的查询应如下所示: -

 "SELECT o.OrderID, o.CustomerID, o.OrderDate, o.ShippedDate, o.Freight, o.customerPayAmount, " +
                "o.ShipName, o.ShipAddress, o.ShipCity, o.ShipPostalCode, o.ShipCountry, " +
                "c.firstName as BillName, c.Address as BillAddress, " +
                "c.City as BillCity, c.PostalCode as BillPostalCode, " +
                "c.Country as BillCountry, s.CompanyName as ShipperName, " +
                "p.ProductName, od.UnitPrice, od.Qty,od.Amount, " +
                "o.discount, o.shippingPackagingCost, od.vat, o.grandTotal, o.remainPayment "+
                "FROM Orders o " +
                "JOIN Customers c ON " +
                "o.CustomerID = c.CustomerID " +
                "JOIN  Shippers s ON " +
                "o.ShipVia = s.ShipperID " +
                "JOIN OrderDetails od ON " +
                " o.OrderID = od.OrderID " +
                "JOIN Products p ON " +
                "od.ProductID = p.ProductID " +
                "WHERE o.OrderID = ? "+
                "group by o.OrderID,c.firstName,s.CompanyName,od.UnitPrice,p.ProductName");

答案 1 :(得分:0)

你需要的是分组。如果您需要返回多个记录,则可能需要使用GROUP_CONCAT函数。

GROUP_CONCAT( p.ProductName )

查看开发人员网站,了解有关GROUP_CONCAT功能的更多信息:

GROUP_CONCAT - MYSQL

答案 2 :(得分:0)

您编写的查询很好,您使用了Distinct,因此它不应该返回重复记录。

请查看表格的原始数据。

应该有重复的数据,如空格或隐形字符......