select table1.orderer_name
from (select o.orderer_name, sum(om.price * om.count) as 결제금액
from order_menu om
inner join `order` o on om.order_id = o.id
group by o.id) as table1
where 결제금액 = (select max(결제금액) from (select o.orderer_name, sum(om.price * om.count) as 결제금액
from order_menu om
inner join `order` o on om.order_id = o.id
group by o.id) as table1);
위처럼 만들면 결과는 나온다. 하지만 중첩되는 부분이 있다.
아래처럼 바꾸면 중첩을 피할 수 있다.
SELECT o.orderer_name
FROM order_menu om
INNER JOIN `order` o ON om.order_id = o.id
GROUP BY o.id
HAVING SUM(om.price * om.count) = (
SELECT MAX(total_price)
FROM (
SELECT SUM(om2.price * om2.count) AS total_price
FROM order_menu om2
GROUP BY om2.order_id
) AS subquery
);