sum.md 1.3 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 求和练习

Joe 想要得到 orders 表

```mysql
create table orders (
                        id int primary key auto_increment,
                        item_id int,
                        amount int,
                        unit_price decimal(12, 4),
                        total decimal(12, 4),
                        description varchar(2000),
                        ts timestamp default now(),
                        deal bool default false
);
```

中所有单价(unit_price)超过 1000 的订单中,已成交(deal 为 true)的总值(total),这个查询应该是:

M
Mars Liu 已提交
20 21
<hr/>

F
feilong 已提交
22
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
23

F
feilong 已提交
24 25
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
26

M
Mars Liu 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
## 答案

```mysql
select sum(total) from orders where deal and unit_price > 1000;
```

## 选项

### A

```mysql
select sum(total) from orders where deal and unit_price > 1000;
```

### B

```mysql
select sum(total) from orders having deal and unit_price > 1000;
```

### C

```mysql
select sum(total) from orders group by deal having unit_price > 1000;
```

### D

```mysql
select sum(total) from orders having deal and unit_price > 1000;
```