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/>

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

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

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

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

## 选项

### 缺少 limit

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

### 缺少 offset

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

### 结构不对

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