我的表中有一个金额transactions
列表。我想找出person_id
总金额大于50
.
我希望这会奏效,但它没有:
SELECT (
SELECT SUM(amount)
FROM transactions WHERE person_id = p.id
) AS total_amount
FROM people AS p
WHERE total_amount > 50
我能让这个工作的唯一方法是:
SELECT (
SELECT SUM(amount)
FROM transactions WHERE person_id = p.id
) AS total_amount
FROM people AS p
WHERE (
SELECT SUM(amount)
FROM transactions WHERE person_id = p.id
) > 50
..这是超级低效的。关于如何更好地格式化查询的任何建议?