网站开发技术

记点笔记、 学点技术 欢迎交流建站技术。本站关注lamp技术

您尚未登录。

#1 2014-11-07 10:02:28

admin
管理员

mysql 用where 进行数据的筛选 in ,not ,and, or

筛选的方式有 

where 条件 ;筛选指定的条件

where and ; 满足多个条件, 可以有 多个and

where or  ; 满足条件之一,即可

where and or ;  用于构造复杂的逻辑, 这里 and , or 和一般的语言一样, and 的优先级高

where in  ; 在指定的范围

where not ; 取反的意思

--------------

示例:

先填写表

mysql> select * from tb001;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
| 2    | x1   |    1 |      3 |
| 2    | x3   |    2 |      3 |
| 3    | x3   |    3 |      4 |
+------+------+------+--------+
5 rows in set (0.00 sec)

ipbbs.net

离线

#2 2014-11-07 10:06:52

admin
管理员

Re: mysql 用where 进行数据的筛选 in ,not ,and, or

mysql> select * from tb001  where type=1;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
+------+------+------+--------+
2 rows in set (0.00 sec)

mysql> select * from tb001 where type=1 and age=2;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x2   |    2 |    2.2 |
+------+------+------+--------+
1 row in set (0.00 sec)

mysql> select * from tb001 where type=1 or type=3;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
| 3    | x3   |    3 |      4 |
+------+------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from tb001 where type=1 or type =3;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
| 3    | x3   |    3 |      4 |
+------+------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from tb001 where type=1 or type =3 and type =1;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
+------+------+------+--------+
2 rows in set (0.00 sec)

mysql> select * from tb001 where type=1 or (type =3 and type =1);
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
+------+------+------+--------+
2 rows in set (0.02 sec)

mysql> select * from tb001 where type=1 and type =3 and type =1;
Empty set (0.00 sec)

mysql> select * from tb001 where type=1 and type =3 or type =1;
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 1    | x2   |    2 |    2.2 |
+------+------+------+--------+
2 rows in set (0.00 sec)

mysql> select * from tb001 where age in (2, 3);
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x2   |    2 |    2.2 |
| 2    | x3   |    2 |      3 |
| 3    | x3   |    3 |      4 |
+------+------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from tb001 where age not in (2, 3);
+------+------+------+--------+
| type | name | age  | weight |
+------+------+------+--------+
| 1    | x1   |    1 |    1.1 |
| 2    | x1   |    1 |      3 |
+------+------+------+--------+
2 rows in set (0.00 sec)



ipbbs.net

离线

页脚

Powered by FluxBB