在MYSQL中迭代地从两个SELECT语句中减去值

时间:2014-12-08 14:31:23

标签: mysql

如何从MySQL中的两个表中迭代地减去两个值?下面的代码适用于一个缺口,但我希望MySQL迭代'points'表中的所有缺口。

以下是一个缺口值的工作代码:

SELECT 
(SELECT SUM(points_log.amount) FROM points_log WHERE nick='prestonconnors' GROUP BY nick)
-
(SELECT SUM(points.amount) FROM points WHERE nick='prestonconnors' GROUP by nick)

提前谢谢你?

1 个答案:

答案 0 :(得分:1)

假设您可以加入这两个表,这可能就是您想要的吗?

SELECT points.nick, SUM(points_log.amount) - SUM(points.amount) 
FROM points 
JOIN points_log ON points.nick = points_log.nick
GROUP BY points.nick