leetcode1068. Product Sales Analysis I (SQL)

Sales Sheet Sales:

+ ------- + ------------- +
| Column the Name | Type |
+ ------------- + ------ - +
| sale_id | int |
| product_id | int |
| year | int |
| the Quantity | int |
|. price | int |
+ ------------- + ------- +
(sale_id, year) sales sales table primary key.
product_id is a foreign key table product of the product.
Note: price represents a price per unit of
product list product:

+ --------- + -------------- +
| Column the Name | Type |
+ -------------- + - + -------
| product_id | int |
| PRODUCT_NAME | VARCHAR |
+ -------------- + --------- +
product_id is a primary key.
write a SQL query statement to get all the products table product name product name and product in the Sales table corresponding to the year and the market year price price.

Example:

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  |
+--------------+-------+-------+

Ideas: joining two tables query.

select product_name, year, price
from Sales,Product
where Sales.product_id=Product.product_id;

 

Published 599 original articles · won praise 10000 + · Views 1.4 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104410829