如何显示前 3 名产品及其价格并按平均价格排列?

How can I display top 3 products with its price and arrange by average prices?

我在产品 table 中有此记录 product_id 及其价格

product_id price
1 150
1 190
2 20
2 12
3 123
4 513
5 157
5 147

我想获得前 3 名的产品并按平均价格排列,如下所示

product_id price avg_price
4 513 513
1 150 170
1 190 170
5 157 152
5 147 152

如何在sql查询或laraveleloquent查询中write/code它?

WITH AverageCTE AS
(
    SELECT product_id, AVG(avg_price) as avg_price 
    FROM products
    GROUP BY product_id
)

SELECT p.product_id, price, avg_price
FROM product p JOIN 
(SELECT * FROM AverageCTE ORDER BY avg_price DESC LIMIT 3) a 
    on p.product_id = a.product_id
ORDER BY avg_price DESC