数据过滤(MySQL)

数据过滤

数据过滤用在WHERE表达式里,常用的有基本查询过滤、条件查询过滤、模糊查询过滤、字段查询过滤以及正则表达式查询过滤。
在这里插入图片描述

一、基本查询过滤

基本查询过滤可以查询所有字段数据或指定一个字段或者多个字段的数据。

附带建表
mysql> create table user (
    ->  id int(11) not null primary key,
    -> name nvarchar(255) default null,
    -> sex nvarchar(255) default null,
    -> age int(11) default null,
    -> password int(11) default null,
    -> phone int(11) default null,
    -> loginname nvarchar(255) default null);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into user values(1,"david","男","19","123456","5555555","hfhhj");
Query OK, 1 row affected (0.01 sec)

mysql> insert into user values(2,"小明","男","22","123456","444444","hfhhj"),( 3,"小花","女","21","123456","6666666","hfhhkj"),(4,"小敏","女","25","123456","8888888","hfhhj"),( 5,"小华","男","26","123456","999999","hfhhj");
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select*from user;
+----+-------+------+------+----------+---------+-----------+
| id | name  | sex  | age  | password | phone   | loginname |
+----+-------+------+------+----------+---------+-----------+
|  1 | david ||   19 |   123456 | 5555555 | hfhhj     |
|  2 | 小明  ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花  ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏  ||   25 |   123456 | 8888888 | hfhhj     |
|  5 | 小华  ||   26 |   123456 |  999999 | hfhhj     |
+----+-------+------+------+----------+---------+-----------+
5 rows in set (0.00 sec)

mysql> select name,sex,age from user;
+-------+------+------+
| name  | sex  | age  |
+-------+------+------+
| david ||   19 |
| 小明  ||   22 |
| 小花  ||   21 |
| 小敏  ||   25 |
| 小华  ||   26 |
+-------+------+------+
5 rows in set (0.00 sec)
二、条件查询过滤

条件查询过滤关键字为AND、OR、IN、NOT IN、IS NULL、IS NOT NULL、BETWEEN AND。按关键字可以进行条件查询过滤。

1、AND(与),两个条件同房时满足的情况下使用。

select*from user where age=20 and name='kevin';

2、OR(或),只要求满足一个条件。

select*from user where age=20 name='kevin';

3、IN(在范围内),在其范围内就可。

select*from user where id in (3,5,7);

4、NOT IN(不在范围内),查询不在其范围内。

select*from user where id not in(3,5,7);

5、IS(为空),查询某字段为空用is null,而不能使用"=null",因为在MySQL中的null不等于任何其它值。

select*from user where name is null;

6、BETWEEN AND(在…区间),查询出来的数据在其区间。

select*from user where age between 10 and 20;
mysql> select*from user where sex='女' and age>20;
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
2 rows in set (0.00 sec)

mysql> select*from user where name='小花'or name='小明';
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  2 | 小明 ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
+----+------+------+------+----------+---------+-----------+
2 rows in set (0.00 sec)

mysql> select*from user where id in(1,3,5);
+----+-------+------+------+----------+---------+-----------+
| id | name  | sex  | age  | password | phone   | loginname |
+----+-------+------+------+----------+---------+-----------+
|  1 | david ||   19 |   123456 | 5555555 | hfhhj     |
|  3 | 小花  ||   21 |   123456 | 6666666 | hfhhkj    |
|  5 | 小华  ||   26 |   123456 |  999999 | hfhhj     |
+----+-------+------+------+----------+---------+-----------+
3 rows in set (0.00 sec)

mysql> select*from user where id not in(1,3,5);
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  2 | 小明 ||   22 |   123456 |  444444 | hfhhj     |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
2 rows in set (0.00 sec)

mysql> select*from user where phone is not null;
+----+-------+------+------+----------+---------+-----------+
| id | name  | sex  | age  | password | phone   | loginname |
+----+-------+------+------+----------+---------+-----------+
|  1 | david ||   19 |   123456 | 5555555 | hfhhj     |
|  2 | 小明  ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花  ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏  ||   25 |   123456 | 8888888 | hfhhj     |
|  5 | 小华  ||   26 |   123456 |  999999 | hfhhj     |
+----+-------+------+------+----------+---------+-----------+
5 rows in set (0.00 sec)

mysql> select*from user where age>20 and age<30;
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  2 | 小明 ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
|  5 | 小华 ||   26 |   123456 |  999999 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
4 rows in set (0.00 sec)

mysql> select*from user where age between 20 and 30;
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  2 | 小明 ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
|  5 | 小华 ||   26 |   123456 |  999999 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
4 rows in set (0.00 sec)

mysql>  select*from user where sex !='男';
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
2 rows in set (0.00 sec)

mysql> select*from user where  sex <> '男';
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
2 rows in set (0.00 sec)
三、模糊查询过滤

模糊查询过滤使用关键字LIKE进行查询。

1、select*from user where name like '张%';
2、select*from user where name like '%明';
3、select*from user where name like '%明%';
mysql> select*from user where name like '小%';
+----+------+------+------+----------+---------+-----------+
| id | name | sex  | age  | password | phone   | loginname |
+----+------+------+------+----------+---------+-----------+
|  2 | 小明 ||   22 |   123456 |  444444 | hfhhj     |
|  3 | 小花 ||   21 |   123456 | 6666666 | hfhhkj    |
|  4 | 小敏 ||   25 |   123456 | 8888888 | hfhhj     |
|  5 | 小华 ||   26 |   123456 |  999999 | hfhhj     |
+----+------+------+------+----------+---------+-----------+
4 rows in set (0.00 sec)

mysql> select*from user where name like '%明';
+----+------+------+------+----------+--------+-----------+
| id | name | sex  | age  | password | phone  | loginname |
+----+------+------+------+----------+--------+-----------+
|  2 | 小明 ||   22 |   123456 | 444444 | hfhhj     |
+----+------+------+------+----------+--------+-----------+
1 row in set (0.00 sec)
四、字段控制查询过滤

字段控制查询过滤可以使用DISTINCT去除重复过滤,使用AS设置别名。

1、DISTINCT:去除重复的列值。

select distinct age from user;

2、AS:可以设置列的别名,也可以省略AS来设置关键字。

select name as 姓名 from user;

或者

select name 姓名 from user;
mysql> select distinct sex from user;
+------+
| sex  |
+------+
||
||
+------+
2 rows in set (0.00 sec)

mysql> select distinct age from user;
+------+
| age  |
+------+
|   19 |
|   22 |
|   21 |
|   25 |
|   26 |
+------+
5 rows in set (0.00 sec)

mysql> select*,id+age from user;
+----+-------+------+------+----------+---------+-----------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id+age |
+----+-------+------+------+----------+---------+-----------+--------+
|  1 | david ||   19 |   123456 | 5555555 | hfhhj     |     20 |
|  2 | 小明  ||   22 |   123456 |  444444 | hfhhj     |     24 |
|  3 | 小花  ||   21 |   123456 | 6666666 | hfhhkj    |     24 |
|  4 | 小敏  ||   25 |   123456 | 8888888 | hfhhj     |     29 |
|  5 | 小华  ||   26 |   123456 |  999999 | hfhhj     |     31 |
+----+-------+------+------+----------+---------+-----------+--------+
5 rows in set (0.00 sec)

mysql> select*,id+age as total from user;
+----+-------+------+------+----------+---------+-----------+-------+
| id | name  | sex  | age  | password | phone   | loginname | total |
+----+-------+------+------+----------+---------+-----------+-------+
|  1 | david ||   19 |   123456 | 5555555 | hfhhj     |    20 |
|  2 | 小明  ||   22 |   123456 |  444444 | hfhhj     |    24 |
|  3 | 小花  ||   21 |   123456 | 6666666 | hfhhkj    |    24 |
|  4 | 小敏  ||   25 |   123456 | 8888888 | hfhhj     |    29 |
|  5 | 小华  ||   26 |   123456 |  999999 | hfhhj     |    31 |
+----+-------+------+------+----------+---------+-----------+-------+
5 rows in set (0.00 sec)

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木头科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值