Mysql 计算sql查询的订单和数量

Mysql 计算sql查询的订单和数量,mysql,Mysql,大家下午好!我对sql和编写查询非常陌生,所以如果有人能仔细检查一下我的工作,并向我传授一些有用的技巧,告诉我如何更好地编写代码?我已经粘贴了查询的目的和我的代码-我感谢您的帮助和反馈谢谢 基本上,该查询应该创建一份关于最近下了总额超过5000美元订单的欧洲客户的报告。订单总额必须反映客户订购的产品数量,还应具有折扣和订单总额 再次感谢你的帮助 select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice)'Total' from

大家下午好!我对sql和编写查询非常陌生,所以如果有人能仔细检查一下我的工作,并向我传授一些有用的技巧,告诉我如何更好地编写代码?我已经粘贴了查询的目的和我的代码-我感谢您的帮助和反馈谢谢

基本上,该查询应该创建一份关于最近下了总额超过5000美元订单的欧洲客户的报告。订单总额必须反映客户订购的产品数量,还应具有折扣和订单总额

再次感谢你的帮助

select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice)'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain', 'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark', 'finland', 'poland') 
group by c.Country, c.CustomerID
having sum ('Total' * od.Quantity) <= 5000
order by total desc
选择c.客户ID、c.国家、金额(od折扣*od单价)'Total'
来自客户c
内部连接顺序
在c.CustomerID=o.CustomerID上
内部连接订单详细信息od
on o.OrderID=od.OrderID
其中c.国家位于(‘德国’、‘英国’、‘瑞典’、‘法国’、‘西班牙’、‘瑞士’、‘奥地利’、‘意大利’、‘比利时’、‘挪威’、‘丹麦’、‘芬兰’、‘波兰’)
按c.国家、c.客户ID分组

有sum('Total'*od.Quantity)看起来不错,但有两点: 1) 您的
总数可能有误
2) 必须反转比较运算符:

select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice * od.Quantity) 'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain', 
  'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark', 
  'finland', 'poland') 
and o.OrderDate >= '2017-03-01' -- for example
group by c.Country, c.CustomerID
having sum ( od.Discount * od.UnitPrice * od.Quantity) >= 5000
order by total desc

我编辑了答案,因为没有与您问题的“最近”部分对应的代码,也许您需要在
WHERE
子句中添加额外的标准。感谢Giorgos,感谢您的帮助和反馈!