1

对于改善广告公司工作流程的项目,您可以使用不同的表进行查询,这些表被使用:

clients: Contains ID, business, lastname, name, addresse, city and zoneID etc
lookupClients: ID, clientID, productID, date, part1, part2, sizeID, design, quant
lookupPrices: ID, productID, sizeID, price
products: ID, artname, image, category
size: ID, size, width, height

为了获得我需要的信息,你做了以下 SQL 查询

SELECT products.ID AS PID, clients.zoneID AS ZID, clients.business, clients.address, size.size, lookupClients.quant, lookupClients.date, lookupClients.part1, lookupClients.part2, lookupPrices.price
FROM lookupClients, lookupPrices, clients, products, size, zone
WHERE lookupClients.clientID = clients.ID
AND lookupClients.sizeID = lookupPrices.sizeID
AND lookupClients.sizeID = size.ID
AND lookupClients.productID = products.ID
AND lookupPrices.productID = products.ID
AND clients.zoneID = zone.ID
ORDER BY clients.zoneID, lookupClients.sizeID, clients.business

到目前为止,这个查询工作正常,我用它在 mysql 中构建一个视图。但问题是,当您想要构建此视图的总和并对列 part1、part2 和 price 求和时,mysql 返回完全错误的结果。有没有人想改进这个查询以获得正确的总和?

感谢您的帮助。

4

1 回答 1

1

像这样添加一个GROUP BY子句:

SELECT 
  p.ID AS PID, 
  c.zoneID AS ZID, 
  c.business, 
  c.address, 
  s.size, 
  lc.quant, 
  lc.date, 
  SUM(lc.part1), 
  SUM(lc.part2), 
  SUM(lp.price)
FROM lookupClients lc 
INNER JOIN lookupPrices lp ON lc.sizeID = lp.sizeID
INNER JOIN clients c ON lc.clientID = c.ID
INNER JOIN products p ON lc.productID = p.Id 
INNER JOIJ size s ON s.ID = lc.sizeId
INNER JOIN zone z ON c.zoneID = z.id
GROUP BY p.ID, c.zoneID, c.business, c.address, s.size, lc.quant, lc.date
ORDER BY c.zoneID, lc.sizeID, c.business
于 2012-11-09T12:07:00.040 回答