呼如林
也许临时表会做你想要的。CREATE TEMPORARY TABLE SalesSummary (product_name VARCHAR(50) NOT NULL, total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00, avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00, total_units_sold INT UNSIGNED NOT NULL DEFAULT 0) ENGINE=MEMORY;INSERT INTO SalesSummary(product_name, total_sales, avg_unit_price, total_units_sold)SELECT
p.name , SUM(oi.sales_amount)
, AVG(oi.unit_price)
, SUM(oi.quantity_sold)FROM OrderItems oiINNER JOIN Products p ON oi.product_id = p.product_idGROUP BY p.name;/* Just output the table */SELECT * FROM SalesSummary;/* OK, get the highest selling product from the table */SELECT product_name AS "Top Seller"FROM SalesSummaryORDER BY total_sales DESCLIMIT 1;/* Explicitly destroy the table */DROP TABLE SalesSummary;来自forge.mysql.com。另请参见本文的临时表。