mysql

利用MySQL整数类型进行数据分析

小樊
84
2024-09-08 13:08:55
栏目: 云计算

在MySQL中,可以使用整数类型(如TINYINT、SMALLINT、MEDIUMINT、INT和BIGINT)来存储整数值

  1. 创建一个包含整数类型的表:
CREATE TABLE sales_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT,
    quantity_sold INT,
    price_per_unit DECIMAL(10, 2)
);
  1. 向表中插入数据:
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);
  1. 查询每个产品的总销售量:
SELECT product_id, SUM(quantity_sold) as total_sales
FROM sales_data
GROUP BY product_id;
  1. 查询每个产品的平均销售价格:
SELECT product_id, AVG(price_per_unit) as average_price
FROM sales_data
GROUP BY product_id;
  1. 查询销售量最高的产品:
SELECT product_id, SUM(quantity_sold) as total_sales
FROM sales_data
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 1;
  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整数类型进行简单的数据分析。实际应用中,你可能需要根据具体需求编写更复杂的查询语句。

0
看了该问题的人还看了