paged.md 893 字节
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 18 19
create table orders
(
    id          serial primary key,
    product_id  integer,
    order_date  date default now(),
    quantity    integer,
    customer_id integer
);
```

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

## 答案

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

## 选项

### 缺少 limit

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

### 缺少 offset

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

### 结构不对

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