paged.md 918 字节
Newer Older
M
Mars Liu 已提交
1 2
# 分页

M
Mars Liu 已提交
3
我们有如下订单表:
M
Mars Liu 已提交
4 5 6 7 8 9 10 11 12 13 14 15

```postgresql
create table orders
(
    id          serial primary key,
    product_id  integer,
    order_date  date default now(),
    quantity    integer,
    customer_id integer
);
```

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

M
Mars Liu 已提交
18
## 答案
M
Mars Liu 已提交
19 20

```postgresql
M
format  
Mars Liu 已提交
21 22 23 24
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100; 
M
Mars Liu 已提交
25 26 27 28
```

## 选项

M
format  
Mars Liu 已提交
29
### 缺少 limit
M
Mars Liu 已提交
30 31

```postgresql
M
format  
Mars Liu 已提交
32 33 34 35
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2; 
M
Mars Liu 已提交
36 37
```

M
format  
Mars Liu 已提交
38
### 缺少 offset
M
Mars Liu 已提交
39 40

```postgresql
M
format  
Mars Liu 已提交
41 42 43
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1; 
M
Mars Liu 已提交
44 45 46 47 48
```

### 结构不对

```postgresql
M
format  
Mars Liu 已提交
49 50 51 52
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
offset $2 and limit 100; 
M
Mars Liu 已提交
53
```