gpt4 book ai didi

mysql - SQL 使用 JOIN 查询多个表 - Northwind

转载 作者:行者123 更新时间:2023-11-29 06:41:27 25 4
gpt4 key购买 nike

//个人理解,非hw赋值所以在 MS 的示例数据库 northwind 中有表:订单 o,[订单详细信息] od,客户 c

  • o 有 orderID、customerID(包括重复项)
  • od 有 orderID(包括重复项)、单价、数量、折扣
  • c 有 customerID,companyName

粗略地说,我要加入

o.customerID = c.customerID; selecting companyName ///
join on o.orderID = od.orderID; selecting unitprice, quantity, discount

我的最终目标是

 sum(q (up - d)) AS 'Order Total' group by od.orderID then
sum(Order Total) group by companyName(?)

不过,我的主要问题是不知道如何/加入什么才能正确加入。

提前致谢

最佳答案

SQL Fiddle 上查看您的场景

SELECT comp.`company_name` AS 'company',COUNT(DISTINCT o.id_sales_order) AS 'total_orders',SUM(`unit_price`) AS 'grand_total'
FROM sales_order AS o
LEFT JOIN sales_order_item AS od ON od.fk_sales_order = o.id_sales_order
LEFT JOIN customer AS c ON c.id_customer = o.fk_customer
LEFT JOIN company AS comp ON comp.id_company = c.fk_company_id
GROUP BY comp.`company_name`

希望这就是你要找的东西

关于mysql - SQL 使用 JOIN 查询多个表 - Northwind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21327621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com