where.md 1.1 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11
# Where 条件

Joe 希望从 orders 表

```mysql
create table orders
(
    id          int primary key auto_increment,
    item_id     int,
    amount      int,
    unit_price  decimal(12, 4),
M
Mars Liu 已提交
12
    price decimal(12, 4),
M
Mars Liu 已提交
13 14 15 16 17 18 19 20
    description varchar(2000),
    ts          timestamp default now(),
    deal        bool      default false
);
```

查询 2022 年 5 月 25 日下单的所有单价低于 20 的订单id,那么这个查询应该如何写?

M
Mars Liu 已提交
21 22
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill)

M
Mars Liu 已提交
23 24 25 26 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 58 59 60 61 62 63 64 65 66
## 答案

```mysql
select id
from orders
where date(ts) = '2022-05-25'
  and unit_prise < 20;
```

## 选项

### A

```mysql
select id
from (select * from orders where date(ts) = '2022-05-25') as o
where unit_prise < 20;
```

### B

```mysql
select id
from orders
where date(ts) = '2022-05-25'
   or unit_prise < 20;
```

### C

```mysql
select id
from orders 
if date(ts) = '2022-05-25' or unit_prise < 20;
```

### D

```mysql
select id
from orders
which date(ts) = '2022-05-25'
   or unit_prise < 20;
```