做網站是干嘛廈門人才網最新招聘信息
文章目錄
- 力扣高頻SQL 50題(基礎版)第七題
- 1068. 產品銷售分析 I
- 題目說明
- 思路分析
- 實現(xiàn)過程
- 準備數(shù)據(jù):
- 實現(xiàn)方式:
- 結果截圖:
- 總結:
力扣高頻SQL 50題(基礎版)第七題
1068. 產品銷售分析 I
題目說明
銷售表 Sales
:
±------------±------+
| Column Name | Type |
±------------±------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
±------------±------+
(sale_id, year) 是銷售表 Sales 的主鍵(具有唯一值的列的組合)。
product_id 是關聯(lián)到產品表 Product 的外鍵(reference 列)。
該表的每一行顯示 product_id 在某一年的銷售情況。
注意: price 表示每單位價格。
產品表 Product
:
±-------------±--------+
| Column Name | Type |
±-------------±--------+
| product_id | int |
| product_name | varchar |
±-------------±--------+
product_id 是表的主鍵(具有唯一值的列)。
該表的每一行表示每種產品的產品名稱。
編寫解決方案,以獲取 Sales
表中所有 sale_id
對應的 product_name以及該產品的所有 year 和 price。
返回結果表 無順序要求 。
思路分析
實現(xiàn)過程
準備數(shù)據(jù):
Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)
Create table If Not Exists Product (product_id int, product_name varchar(10))
Truncate table Sales
insert into Sales (sale_id, product_id, year, quantity, price) values ('1', '100', '2008', '10', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('2', '100', '2009', '12', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('7', '200', '2011', '15', '9000')
Truncate table Product
insert into Product (product_id, product_name) values ('100', 'Nokia')
insert into Product (product_id, product_name) values ('200', 'Apple')
insert into Product (product_id, product_name) values ('300', 'Samsung')
實現(xiàn)方式:
select p.product_name,s.year,s.price from Sales s,Product p where s.product_id=p.product_id;
結果截圖:
總結:
#隱式內連接語法
select 字段名 from A表名, B表名 where 條件;