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

标签 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. shipperid下的shippers表为主键

[![sql select query下的图片带输出,便于理解][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

最佳答案

你写的查询没问题,你已经使用了 Distinct 所以它不应该返回重复的记录。

请检查您的表的原始数据。

应该有重复的数据,但有细微的变化,比如空格或不可见字符......

关于java - 为什么这些连接查询返回重复记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591270/

相关文章:

svg - Inkscape 导出到 Core 图形?

java - 如何在 Windows 7 上使用 MySQL 作为后端设计器

java.util.AbstractMap.equals() : what is it for try/catch?

java - 为什么要编译?类型删除

mysql - 有关电影主题的 sql 遇到问题

MySQL用分组连接没有完美连接

linux - 如何制作删除文件的shell脚本

java - Java中串口参数的含义?

java - 为简单的 Java Web 应用程序使用框架是否有意义?

php - 我尝试使用 random() 函数在 mysql 中创建表。?