在其中选择差异的对象

时间:2018-10-31 14:13:16

标签: mysql

我试图找到一种方法来运行它:

SELECT 
    s.system_number,
    s.total_points,
    p.licensed_points,
    SUM(p.licensed_points) - SUM(s.total_points) AS difference_points
FROM 
    server AS s 
    LEFT JOIN 
        practice AS p ON p.system_number = s.system_number 
WHERE 
    difference_points < 0

我的目标是仅选择差异点<0的对象

有人知道该怎么做吗?

谢谢

1 个答案:

答案 0 :(得分:1)

  • 在不使用Sum()的情况下使用聚合函数Group By毫无意义。您将需要在Group By上进行s.system_number
  • 然后,您可以使用Having子句来考虑difference_points小于0的情况。

请尝试以下操作:

SELECT 
    s.system_number,
    SUM(s.total_points),
    SUM(p.licensed_points),
    SUM(p.licensed_points) - SUM(s.total_points) AS difference_points
FROM 
    server AS s 
    LEFT JOIN 
        practice AS p ON p.system_number = s.system_number 
GROUP BY 
  s.system_number 
HAVING 
    difference_points < 0