为什么这些连接查询 return 重复记录?
Why these join queries return duplicate records?
Table结构:
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查询:
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
- ordertable下orderid是一个主键
- customerid下的customertable为主键
- shippers table shipperid 下为主键
[![sql下的图片select查询带输出,便于理解][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
您正在寻找以下群组:
您的查询应如下所示:-
"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");
你需要的是分组依据。如果您需要 return 多条记录,您可能需要使用 GROUP_CONCAT 函数。
GROUP_CONCAT( p.ProductName )
查看开发者网站以获取有关 GROUP_CONCAT 功能的更多信息:
你写的查询很好,你已经使用了 Distinct 所以它不应该 return 重复记录。
请检查您的table的原始数据。
应该有重复的数据,但有细微的变化,例如 space 或不可见字符...
Table结构:
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查询:
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
- ordertable下orderid是一个主键
- customerid下的customertable为主键
- shippers table shipperid 下为主键
[![sql下的图片select查询带输出,便于理解][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
您正在寻找以下群组:
您的查询应如下所示:-
"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");
你需要的是分组依据。如果您需要 return 多条记录,您可能需要使用 GROUP_CONCAT 函数。
GROUP_CONCAT( p.ProductName )
查看开发者网站以获取有关 GROUP_CONCAT 功能的更多信息:
你写的查询很好,你已经使用了 Distinct 所以它不应该 return 重复记录。
请检查您的table的原始数据。
应该有重复的数据,但有细微的变化,例如 space 或不可见字符...