MySQL基础练习题14-产品销售分析1

题目:获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。

准备数据

分析数据


题目:获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。

准备数据

## 创建库
create database db;
use db;

## 创建销售表
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');

输入表:

销售表

产品表

 

 分析数据

使用内连接将两张表连接起来

###  以获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。
select product_name,year,price from sales s join product p on p.product_id = s.product_id;

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值