首页 技术 正文
技术 2022年11月21日
0 收藏 963 点赞 3,172 浏览 2393 个字

WHERE约束

where字句中可以使用:

    1. 比较运算符:>< >=  <=  !=
    2. between 80 and 100 值在80到100之间   >=80  <=100
    3. in(80,90,100) 值是80或90或100    满足这个条件就可以
    4. like ‘egon%’
      pattern可以是%或_,
      %表示任意多字符
      _表示一个字符
    5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not

select id,name,age from employee where id >7;

执行顺序 1.from employee  2. where id >7 3. id,name,age

先找到表 再按照约束条件 从表里取要找的记录

单个条件

mysql> select id,name,age from employee where id >7;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 8 | 丫丫 | 38 |
| 9 | 丁丁 | 18 |
| 10 | 星星 | 18 |
| 11 | 格格 | 28 |
| 12 | 张野 | 28 |
| 13 | 程咬金 | 18 |
| 14 | 程咬银 | 18 |
| 15 | 程咬铜 | 18 |
| 16 | 程咬铁 | 18 |
+----+-----------+-----+
9 rows in set (0.11 sec)

找出薪资大于8000的老师

多个条件

mysql> select name,post,salary from employee where post='teacher' and salary>8000;
+------------+---------+------------+
| name | post | salary |
+------------+---------+------------+
| alex | teacher | 1000000.31 |
| jingliyang | teacher | 9000.00 |
| jinxin | teacher | 30000.00 |
| 成龙 | teacher | 10000.00 |
+------------+---------+------------+
4 rows in set (0.00 sec)

薪资大于等于20000 小于等于30000

mysql> select name,salary from employee where  salary >= 20000 and salary <= 30000;
+-----------+----------+
| name | salary |
+-----------+----------+
| jinxin | 30000.00 |
| 程咬金 | 20000.00 |
+-----------+----------+
2 rows in set (0.00 sec)

between 20000 and 30000

原理 >= 20000  <=30000

mysql> select name,salary from employee where salary between 20000 and 30000;
+-----------+----------+
| name | salary |
+-----------+----------+
| jinxin | 30000.00 |
| 程咬金 | 20000.00 |
+-----------+----------+
2 rows in set (0.00 sec)

小于20000 或者大于30000

mysql> select name,salary from employee where salary < 20000 or salary >30000;
+------------+------------+
| name | salary |
+------------+------------+
| alex | 1000000.31 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
14 rows in set (0.00 sec)

between 取反

小于2000   大于30000

mysql> select name,salary from employee where salary not between 20000 and 30000;
+------------+------------+
| name | salary |
+------------+------------+
| alex | 1000000.31 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
14 rows in set (0.00 sec)
#1:单条件查询
SELECT name FROM employee
WHERE post='sale';#2:多条件查询
SELECT name,salary FROM employee
WHERE post='teacher' AND salary>10000;#3:关键字BETWEEN AND
SELECT name,salary FROM employee
WHERE salary BETWEEN 10000 AND 20000; SELECT name,salary FROM employee
WHERE salary NOT BETWEEN 10000 AND 20000;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918