0
我有一個相當大的MYSQL數據庫,我需要平均所有的發票,然後查詢這些平均值的最高值。我的查詢將加入三個表格以獲取適當的數據以供顯示。這些表格是品牌,產品和線路。我需要的列是來自品牌的brand_name和brand_id,來自line的line_price,產品會加入品牌和庫存表。MySQL AVG然後按結果排序
SELECT brand.brand_id, brand.brand_name, brand.brand_type, ROUND(MAX(line_price),2) AS 'Highest AVG Price'
FROM (SELECT brand.brand_id, brand_name, brand_type, AVG(line_price) AS line_price FROM product, brand, line
WHERE product.prod_sku = line.prod_sku
AND product.brand_id = brand.brand_id
GROUP BY brand_id)w1, brand
WHERE w1.brand_id = brand.brand_id;
當我運行查詢時,我得到正確的值,但錯誤的brand_name和brand_id。我究竟做錯了什麼?
你可以給你當前的'SELECT'嗎? – 2015-04-02 23:16:01