Verified Commit 544f6d30 authored by Kiryuu Sakuya's avatar Kiryuu Sakuya 🎵
Browse files

Finish No.4

parent b548a3d2
......@@ -36,7 +36,7 @@ SHOW FULL COLUMNS FROM sales;
SELECT * FROM sales WHERE tot_amt >= 100;
```
#### 5. 选取订单金额由高到低排序的前 10% 订单数据
#### 5. 选取订单金额由高到低排序的前 20% 订单数据
```mysql
SHOW FULL COLUMNS FROM sales;
......@@ -44,17 +44,51 @@ SHOW FULL COLUMNS FROM sales;
# 排序从小到大输出可以用 SELECT * from sales ORDER BY tot_amt ASC;
# 看前三条就是 SELECT * from sales ORDER BY tot_amt DESC LIMIT 3;
# 看随机的 10% 就是 SELECT * from sales WHERE RAND() < 0.1 ORDER BY tot_amt;
SET @sql := CONCAT( 'SELECT * FROM sales ORDER BY tot_amt DESC LIMIT '
, (SELECT ROUND(COUNT(*) * 0.2) FROM sales));
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
```
#### 6. 查找出职称为经理或职称为会计的女员工的信息
```mysql
SHOW FULL COLUMNS FROM employee;
SELECT * FROM employee WHERE emp_sex="女" AND emp_title LIKE "%经理" OR emp_title LIKE "%会计";
```
#### 7. 删除 sales 表中作废的订单(其发票号码为 34600376)
> 注意约束!
本处,因为 `order_no` 有了外键引用,所以子表为 `sales` 表,父表为 `sale_item` 表,删除先删除子表数据。
```mysql
SELECT * FROM sales WHERE invoice_no="34600376";
DELETE FROM sales WHERE order_no="11035462019111100000020957";
DELETE FROM sale_item WHERE order_no="11035462019111100000020957";
```
#### 8. 计算出一共销售了几种产品
#### 9. 显示 sale_item 表中每种个别产品的订购金额总和,并且一句销售金额由大到小排列来显示出每一种产品的排行榜
```mysql
SELECT COUNT(DISTINCT prod_id) as "共销售产品数" FROM sale_item;
```
#### 9. 显示 sale_item 表中每种个别产品的订购金额总和,并且依据销售金额由大到小排列来显示出每一种产品的排行榜
```mysql
SELECT prod_id AS "产品编号", SUM(qty * unit_price) AS "销售总额" FROM sale_item GROUP BY prod_id ORDER BY SUM(qty * unit_price) DESC;
```
#### 10. 计算每一产品每月的销售金额综合,并将结果按销售月份及产品编号排序
```mysql
# 仅仅按年月输出总销售金额并按月排序 select YEAR(order_date) AS "年份", MONTH(order_date) AS "月份", SUM(tot_amt) AS "销售总额" FROM sales GROUP BY MONTH(order_Date);
SELECT prod_id AS "产品编号", MONTH(order_date) AS "月份", SUM(qty * unit_price) AS "销售总额" FROM sale_item GROUP BY prod_id ORDER BY MONTH(order_Date) ASC, prod_id ASC;
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment