在没有子查询的情况下重写sql查询

时间:2013-07-11 15:32:44

标签: mysql join subquery

我需要一些关于以下MySQL查询的帮助

我想在没有任何子查询的情况下重写我的查询 - 但我不知道如何...

这是一个视频租赁商店数据库;)

SELECT k.first_name, k.last_name, SUM(amount) AS profit 
FROM payment AS p 
JOIN (SELECT c.* FROM customer AS c 
 JOIN rental AS r ON c.customer_id = r.customer_id 
WHERE r.return_date IS NULL GROUP BY c.customer_id HAVING 
COUNT(*) > '1') AS k ON p.customer_id = k.customer_id 
GROUP BY k.customer_id 
HAVING SUM(amount) > 100 
ORDER BY profit DESC;

谢谢:)

1 个答案:

答案 0 :(得分:0)

这个:

 SELECT c.first_name, c.last_name, c.customer_id,SUM(amount) AS profit 
 FROM payment AS p 
    INNER JOIN (customer c
        INNER join rental r
        ON c.customer_id=r.customer_id)
    ON p.customer_id = c.customer_id
 GROUP BY c.customer_id,c.last_name,c.first_name
 HAVING SUM(amount) > 100 
 ORDER BY profit DESC;

使用INNER JOIN会将此查询仅限制为已付款的客户。