# Write your MySQL query statement below
1068. 产品销售分析 I
销售表 Sales
:
+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+ (sale_id, year) 是销售表 Sales 的主键. product_id 是关联到产品表 Product 的外键. 注意: price 表示每单位价格
产品表 Product
:
+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+ product_id 是表的主键.
写一条SQL 查询语句获取 Sales
表中所有产品对应的 产品名称 product_name 以及该产品的所有 售卖年份 year 和 价格 price 。
查询结果中的顺序无特定要求。
查询结果格式示例如下:
Sales
表:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+---------+------------+------+----------+-------+
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+
Result 表:
+--------------+-------+-------+
| product_name | year | price |
+--------------+-------+-------+
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |
+--------------+-------+-------+
原站题解
pythondata 解法, 执行用时: 348 ms, 内存消耗: 62.5 MB, 提交时间: 2023-09-17 10:40:28
import pandas as pd def sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame: ans = sales.merge(product, how='inner', on='product_id') return ans[['product_name','year','price']] def sales_analysis2(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame: ans = pd.merge(sales, product, how='left', on='product_id') return ans[['product_name','year','price']]
mysql 解法, 执行用时: 1060 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:51:31
# Write your MySQL query statement below select product_name, year, price from sales, product where sales.product_id = product.product_id
mysql 解法, 执行用时: 864 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:50:55
# Write your MySQL query statement below select p.product_name, s.year, s.price from Sales s left join Product p on s.product_id=p.product_id;