10、MySQL的数据过滤

本文介绍了MySQL中使用WHERE子句进行条件过滤,包括等于、不等于、大于、小于以及BETWEEN操作符的用法。通过示例展示了如何筛选价格为2.5的产品,以及价格在2.5到10之间的产品。同时,也提到了与ORDER BY子句结合使用进行数据排序的方法。
摘要由CSDN通过智能技术生成

where条件过滤
找出价格为2.5的产品

mysql> select * from products where prod_price = 2.5;
+---------+---------+---------------+------------+--------------------------------------+
| prod_id | vend_id | prod_name     | prod_price | prod_desc                            |
+---------+---------+---------------+------------+--------------------------------------+
| FC      |    1003 | Carrots       | 2.50       | Carrots (rabbit hunting season only) |
| TNT1    |    1003 | TNT (1 stick) | 2.50       | TNT, red, single stick               |
+---------+---------+---------------+------------+--------------------------------------+
2 rows in set (0.07 sec)

where条件的操作符
= > < ( <>、 != 都是不等于) between

mysql> select * from products where prod_price > 2.5 and prod_price <= 10;
+---------+---------+----------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name      | prod_price | prod_desc                                                      |
+---------+---------+----------------+------------+----------------------------------------------------------------+
| ANV01   |    1001 | .5 ton anvil   | 5.99       | .5 ton anvil, black, complete with handy hook                  |
| ANV02   |    1001 | 1 ton anvil    | 9.99       | 1 ton anvil, black, complete with handy hook and carrying case |
| FB      |    1003 | Bird seed      | 10.00      | Large bag (suitable for road runners)                          |
| FU1     |    1002 | Fuses          | 3.42       | 1 dozen, extra long                                            |
| OL1     |    1002 | Oil can        | 8.99       | Oil can, red                                                   |
| SLING   |    1003 | Sling          | 4.49       | Sling, one size fits all                                       |
| TNT2    |    1003 | TNT (5 sticks) | 10.00      | TNT, red, pack of 10 sticks                                    |
+---------+---------+----------------+------------+----------------------------------------------------------------+
7 rows in set (0.09 sec)

between

mysql> select * from products where prod_price between 2.5 and 10;
+---------+---------+----------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name      | prod_price | prod_desc                                                      |
+---------+---------+----------------+------------+----------------------------------------------------------------+
| ANV01   |    1001 | .5 ton anvil   | 5.99       | .5 ton anvil, black, complete with handy hook                  |
| ANV02   |    1001 | 1 ton anvil    | 9.99       | 1 ton anvil, black, complete with handy hook and carrying case |
| FB      |    1003 | Bird seed      | 10.00      | Large bag (suitable for road runners)                          |
| FC      |    1003 | Carrots        | 2.50       | Carrots (rabbit hunting season only)                           |
| FU1     |    1002 | Fuses          | 3.42       | 1 dozen, extra long                                            |
| OL1     |    1002 | Oil can        | 8.99       | Oil can, red                                                   |
| SLING   |    1003 | Sling          | 4.49       | Sling, one size fits all                                       |
| TNT1    |    1003 | TNT (1 stick)  | 2.50       | TNT, red, single stick                                         |
| TNT2    |    1003 | TNT (5 sticks) | 10.00      | TNT, red, pack of 10 sticks                                    |
+---------+---------+----------------+------------+----------------------------------------------------------------+
9 rows in set (0.10 sec)

与order by连用
空值检查

mysql> select * from products where prod_desc is null;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值