【问题标题】:Need to know how to select customer names where their purchase price is greater than 100需要知道如何选择购买价格大于100的客户名称
【发布时间】:2019-12-04 17:54:43
【问题描述】:

我有一个 sql 作业。我已经建立了一个包含 3 个表的小型数据库(在图像中)。我需要选择上个月内购买超过 100 的客户名称。所有购买都是分开的。

我尝试使用 SUM

SELECT  customer.CustomerName
FROM customer INNER JOIN
     sales
     ON customer.id=sales.CustomerId
HAVING SUM(sales.SalesPrice > 100)  

在我的数据库中有客户的销售价格总和大于 0 但 SQL 返回空白 outputenter image description here

【问题讨论】:

  • 你不见了GROUP BY
  • 感谢您的快速响应,但没有运气。添加了 GROUP BY customer.id ASC 但收到此消息 #1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“GROUP BY customer.id LIMIT 0, 25”附近使用正确的语法
  • 可能在使用不熟悉的语法时阅读手册。
  • run this SELECT customer.CustomerName FROM customer INNER JOIN sales ON customer.id=sales.CustomerId GROUP BY customer.CustomerName HAVING SUM(sales.SalesPrice > 100) 但我仍然收到空白输出。跨度>

标签: mysql sql sum where-clause having


【解决方案1】:

试试这个:

SELECT customer.customerName FROM customer 
INNER JOIN sales ON customer.id = sales.customerId
GROUP BY customerName
HAVING SUM(sales.SalesPrice) > 100;

【讨论】:

    【解决方案2】:

    正确的语法如下所示:

    SELECT c.CustomerName
    FROM customer c INNER JOIN
         sales s
         ON c.id = s.CustomerId
    GROUP BY c.id, c.CustomerName
    HAVING SUM(s.SalesPrice) > 100;  
    

    【讨论】:

    • @SimasKasparaitis 。 . .您是否有理由不接受此答案?
    • 两个答案都很好。我会接受他们两个 :)) 但 gautam 的声誉较低,所以我决定把这个给他
    【解决方案3】:

    我需要选择上个月购买超过 100 个的客户名称

        SELECT C.CustomerName
          FROM customer AS C INNER JOIN sales AS S ON C.id = S.CustomerId
         WHERE S.SalesDate >= '20190701' AND S.SalesDate < '20190801' --within last month
      GROUP BY C.CustomerName
        HAVING SUM(S.SalesPrice) > 100   --Sum of purchases greater than 100
    

    这种需要查看图片链接,它应该作为问题的一部分包含在内,而不是作为链接。例如,链接可能会过期或链接可能被组织的防火墙阻止。

    【讨论】:

      【解决方案4】:

      此任务的第二部分需要我编写一个查询,该查询返回具有最多事务计数的项目名称。我需要知道哪个ItemId.Sales 的价值最高。目前我有这个代码

      SELECT ItemName 
      FROM item 
      INNER JOIN sales ON item.id = sales.ItemID 
      

      但我需要知道如何计算那些ItemID 以及哪个ID 的计数最多

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-06
        • 2015-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多