我已将表格连接在一起
SELECT od.EmployeeID, nv.LastName, nv.FirstName, od.OrderDate, od.ShipperID, od.OrderID, prod.ProductName, ord.Quantity from Orders as od
INNER JOIN Employees as nv
ON od.EmployeeID = nv.EmployeeID
INNER JOIN Shippers as ship
ON od.ShipperID = ship.ShipperID
INNER JOIN OrderDetails as ord
ON od.OrderID = ord.OrderID
INNER JOIN Products as prod
on prod.ProductID = ord.ProductID
WHERE od.EmployeeID = '8' and date(od.OrderDate) >= date('1997-01-01') and ship.ShipperID = '2'
结果:
如果您使用命令“GROUP BY od.OrderID”
但我想要的结果是只有“OrderID”列被分组。
ProductName 列将完整显示(以逗号分隔),如下所示:
Chang、Gustafs Knäckebröd、乔瓦尼马苏里拉奶酪
使用
GROUP_CONCAT()
并通过 OrderID
聚合:
SELECT od.OrderID, GROUP_CONCAT(prod.ProductName) AS products
FROM Orders AS od
INNER JOIN OrderDetails AS ord
ON od.OrderID = ord.OrderID
INNER JOIN Products AS prod
ON prod.ProductID = ord.ProductID
GROUP BY od.OrderID;