在where中选择具有diff的对象

9bfwbjaz  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(262)

我想找到一个方法来运行这个:

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的对象
有人知道怎么做吗?
谢谢您

pod7payv

pod7payv1#

使用聚合函数 Sum() 没有 Group By 在这里没有意义。你需要做一个 Group Bys.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

相关问题