mysql - SQL Server - 返回 null 的 SELECT 语句

标签 mysql sql select

下面的 SELECT 语句产生了这个结果

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE ProductID NOT IN (1001)
GROUP BY ProductID;

enter image description here

为什么下面编辑的 SELECT 语句没有产生任何结果?

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE Quantity >= 1800
GROUP BY ProductID;

enter image description here

最佳答案

您添加了 WHERE 子句:

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE Quantity >= 1800  -- no single row with Quantity that is higher than 1800
GROUP BY ProductID;

您可能想在聚合后过滤值:

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
GROUP BY ProductID
HAVING SUM(Quantity) >= 1800;   -- HAVING instead of WHERE

过滤:

  • WHERE - 聚合前
  • HAVING - 聚合后

关于mysql - SQL Server - 返回 null 的 SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483456/

相关文章:

javascript - 将 jQuery ReplaceWith() 与 HTML Select 一起使用

select - Dojo dgrid :(预)在渲染中选择行

mysql - mysql 连接的重复结果

mysql - 在具有自动递增 ID 的相同表之间复制行

php - 如何使用 Laravel 在控制台图表中传递值

sql - 想要执行sp_send_dbmail并以excel格式发送结果

c# - 右键单击以选择列表框中的项目

php - 持久循环 mySQL

php 的 mysql_real_escape_string 不起作用?

sql - 如何一次最小化所有 SQL 存储过程