mysql 数据操作 单表查询 where约束 between and or

 

 

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;

 

上一篇:RTKLIB_E.4


下一篇:ECS训练营Day3