在MySQL中,可以使用整數類型(如TINYINT、SMALLINT、MEDIUMINT、INT和BIGINT)來存儲整數值
CREATE TABLE sales_data (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
quantity_sold INT,
price_per_unit DECIMAL(10, 2)
);
INSERT INTO sales_data (product_id, quantity_sold, price_per_unit)
VALUES (1, 10, 99.99),
(2, 5, 49.99),
(3, 20, 199.99),
(1, 30, 99.99),
(2, 15, 49.99);
SELECT product_id, SUM(quantity_sold) as total_sales
FROM sales_data
GROUP BY product_id;
SELECT product_id, AVG(price_per_unit) as average_price
FROM sales_data
GROUP BY product_id;
SELECT product_id, SUM(quantity_sold) as total_sales
FROM sales_data
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 1;
SELECT product_id, SUM(quantity_sold * price_per_unit) as total_revenue
FROM sales_data
GROUP BY product_id
ORDER BY total_revenue DESC
LIMIT 1;
這些示例展示了如何使用MySQL整數類型進行簡單的數據分析。實際應用中,你可能需要根據具體需求編寫更復雜的查詢語句。