paged.md 1.1 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4
# 分页

我们有如下订单表:

M
having  
Mars Liu 已提交
5
```mysql
M
Mars Liu 已提交
6 7 8 9 10 11 12 13 14 15 16 17
create table orders
(
    id          serial primary key,
    product_id  integer,
    order_date  date default now(),
    quantity    integer,
    customer_id integer
);
```

现在开发人员希望查询指定的某一天内的数据,并按每一百条一页查询,那么正确的语句应该是:

M
Mars Liu 已提交
18 19
<hr/>

M
Mars Liu 已提交
20
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill)
M
Mars Liu 已提交
21 22
* `show databases` 列出所有数据库
* `show tables` 列出所有表
M
Mars Liu 已提交
23

M
Mars Liu 已提交
24 25
## 答案

M
having  
Mars Liu 已提交
26
```mysql
M
Mars Liu 已提交
27 28 29 30 31 32 33 34 35 36
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100; 
```

## 选项

### 缺少 limit

M
having  
Mars Liu 已提交
37
```mysql
M
Mars Liu 已提交
38 39 40 41 42 43 44 45
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2; 
```

### 缺少 offset

M
having  
Mars Liu 已提交
46
```mysql
M
Mars Liu 已提交
47 48 49 50 51 52 53
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1; 
```

### 结构不对

M
having  
Mars Liu 已提交
54
```mysql
M
Mars Liu 已提交
55 56 57 58 59
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
offset $2 and limit 100; 
```