MySQL-查询表数据&函数

查询语法:

select 主查询,主要查询语句都需要它

select 属性列表 from 表名 [ where 条件表达式1]
[group by 属性名 [ Having 条件表达式2]] ->分组
[order by 属性名 [ASC|DESC]] ->排序

*表示查询全部属性

示例:
// 全部属性
mysql> select * from Student;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
5 rows in set (0.05 sec)

//指定属性
mysql> select SID,Sname from Student;
+-----+-------+
| SID | Sname |
+-----+-------+
|   1 | 赵雷  |
|   2 | 钱电  |
|   3 | 孙风  |
|   4 | 吴兰  |
|   5 | 孙兰  |
+-----+-------+
5 rows in set (0.00 sec)

合并查询结果:

union(联合) 关键字和 union all关键字都可以合并查询结果,但是两者有一点区别,union关键字合并查询结果时,需要将相同的记录消除掉。而union all关键字则相反,不会消除相同的记录,而是将所有的记录合并在一起。

mysql> select SID from Student union select SID from SC;
+------+
| SID  |
+------+
|    4 |
|    5 |
|    3 |
|    1 |
|    2 |
|    6 |
+------+
6 rows in set (0.00 sec)

mysql> select SID from Student union  all select SID from SC;
+------+
| SID  |
+------+
|    4 |
|    5 |
|    3 |
|    1 |
|    2 |
|    1 |
|    1 |
|    1 |
|    2 |
|    2 |
|    2 |
|    3 |
|    3 |
|    3 |
|    4 |
|    4 |
|    4 |
|    5 |
|    5 |
|    6 |
+------+
20 rows in set (0.04 sec)

子查询

in:

in(元素1,元素2…) 包含括号里面元素的列表,
not in(元素1,元素2…)不包含括号里面元素里面的列表

示例:
mysql> select * from Student where SID in(1,3,4);
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   3 | 孙风  |   21 ||
|   4 | 吴兰  |   18 ||
+-----+-------+------+------+
3 rows in set (0.04 sec)

//不包括
mysql> select * from Student where SID not in(1,3,4);
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   2 | 钱电  |   20 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)
带比较运算符的子查询
select * from SC where SID=(select SID from Student where Sage=20);
带exists关键字的子查询

exists关键字表示存在。使用exists关键字时,内层查询语句不返回查询的记录。而是返回一个真假值。如果内层查询语句查询到满足条件的记录,就返回一个真值,否则返回一个假值。当返回的值为true时,外层查询语句将进行查询,当返回为false时,外层语句不进行查询或者查询不出记录。

mysql> select * from SC where exists(select * from Student where SID=10);
Empty set (0.00 sec)

mysql> select * from SC where exists(select * from Student where SID=1);
+------+------+-------+
| SID  | CID  | score |
+------+------+-------+
|    1 |    1 |    80 |
|    1 |    2 |    71 |
|    1 |    3 |    87 |
|    2 |    1 |    88 |
|    2 |    2 |    70 |
|    2 |    3 |    89 |
|    3 |    1 |    68 |
|    3 |    2 |    78 |
|    3 |    3 |    87 |
|    4 |    1 |    67 |
|    4 |    2 |    58 |
|    4 |    3 |    89 |
|    5 |    1 |    56 |
|    5 |    2 |    89 |
|    6 |    3 |    38 |
+------+------+-------+
15 rows in set (0.00 sec)
带any关键字的子查询:

any关键字表示满足其中任何一个条件,使用any关键字时,只要满足内层查询语句返回结果中的任何一个,就可以通过该条件来执行外层查询语句。
any通常与比较运算符一起使用 如>ANY 表示大于任何一个值,=any表示等于任何一个值

//只要内层返回的结果中,有一个能满足外层的条件,那么内层返回的所有结果都会被显示。
mysql> select * from Course;
+-----+-------+------+
| CID | Cname | TID  |
+-----+-------+------+
|   1 | 语文  |    2 |
|   2 | 数学  |    1 |
|   3 | 英语  |    3 |
+-----+-------+------+
3 rows in set (0.05 sec)

mysql> select * from Teacher;
+-----+-------+
| TID | Tname |
+-----+-------+
|   1 | 张三  |
|   2 | 李四  |
|   3 | 王五  |
+-----+-------+
3 rows in set (0.05 sec)

mysql> select * from Course where TID>any(select TID from Teacher where TID>=!<2);
+-----+-------+------+
| CID | Cname | TID  |
+-----+-------+------+
|   1 | 语文  |    2 |
|   3 | 英语  |    3 |
+-----+-------+------+
2 rows in set (0.00 sec)
带all关键字的子查询:

all关键字表示满足所有条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才能执行外层查询语句。all关键字也可以与比较运算符一起使用。

//通俗来说过,内层返回的结果,只有满足外层的条件才可以
mysql> select * from Course where TID>all(select TID from Teacher where TID>=!<2);
Empty set (0.00 sec)

mysql> select * from Course where TID>all(select TID from Teacher where TID=2);
+-----+-------+------+
| CID | Cname | TID  |
+-----+-------+------+
|   3 | 英语  |    3 |
+-----+-------+------+
1 row in set (0.00 sec)

between and :范围查询

判断某个字段的值是否在指定范围内。
[not] between 取值1 and 取值2
取值1为起始值,取值2为终止值

示例:
mysql> select * from Student where Sage between  20 and 21;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
+-----+-------+------+------+
3 rows in set (0.07 sec)

like:通配符配置查询

[not] lilke ’字符串’
在匹配完整字符串时,=的作用与like一样,但是不能适用通配符

通配符:
  • % :表示0个或者任意长度的字符串
  • _ :只表示单个字符
示例:
mysql> select * from Student where Sname like '%';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
5 rows in set (0.04 sec)

mysql> select * from Student where Sname like '_兰';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)

//不包含
mysql> select * from Student where Sname not like  '%兰%';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
+-----+-------+------+------+
3 rows in set (0.00 sec)

//like与=的比较
mysql> select * from Student where Sname='孙兰';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
1 row in set (0.00 sec)

//=不能缺省查询
mysql> select * from Student where Sname='孙%';
Empty set (0.00 sec)

空值查询:

判断字段的值是否有空值
is [not] null

实例:
mysql> select * from Student where Sname is null;
Empty set (0.00 sec)

mysql> select * from Student where Sname is not null;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
5 rows in set (0.00 sec)

带and的多条件查询:

用来进行联合查询,只有同时满足所有的查询条件的记录才被查询出来。
条件表达式1 and 条件表达式2 [and 条件表达式n]

扫描二维码关注公众号,回复: 6781543 查看本文章
实例:
mysql> select * from Student where Sname like '赵%' and Sage=20;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
+-----+-------+------+------+
1 row in set (0.00 sec)

mysql> select * from Student where Sname='钱电' and Sage=20;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   2 | 钱电  |   20 ||
+-----+-------+------+------+
1 row in set (0.00 sec)

带or的多条件查询:

只要满足查询条件中的其中一个,这样的记录就会被查询出来。
条件表达式1 or 条件表达式2 [or 条件表达式n]
or可以和and一起使用,当两者一起使用时,and比or优先

示例:
mysql> select * from Student where   SID=5 or Sage=20;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
3 rows in set (0.00 sec)

//范围+and查询
mysql> select * from Student where Sage between 18 and 20 and Sname='吴兰';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   4 | 吴兰  |   18 ||
+-----+-------+------+------+
1 row in set (0.00 sec)

// 前面的and和后面的or是并列的关系
mysql> select * from Student where Sage between 18 and 20 and Sname='吴兰' or Sname='赵雷';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   4 | 吴兰  |   18 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)
mysql> select * from Student where Sage between 18 and 20 and Sname='吴兰' or Sname='孙兰';
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)

去重查询:

select distinct 属性名 from 表名

示例:
mysql> select distinct Sage from Student;
+------+
| Sage |
+------+
|   20 |
|   21 |
|   18 |
|   17 |
+------+
4 rows in set (0.06 sec)

对结果排序:

order by 属性名 ASC|DESC
默认为ASC:升序
DESC: 降序
注意,若排序数据中有null,null的权重最小,在ASC中第一个显示
在DESC中最后一个显示

示例:
mysql> select * from Student order by Sage;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   5 | 孙兰  |   17 ||
|   4 | 吴兰  |   18 ||
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
+-----+-------+------+------+
5 rows in set (0.00 sec)

mysql> select * from Student order by Sage DESC;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   3 | 孙风  |   21 ||
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   4 | 吴兰  |   18 ||
|   5 | 孙兰  |   17 ||
+-----+-------+------+------+
5 rows in set (0.00 sec)

mysql> select * from Student order by Sage ASC;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   5 | 孙兰  |   17 ||
|   4 | 吴兰  |   18 ||
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
+-----+-------+------+------+
5 rows in set (0.00 sec)

分组查询

将查询结果按某个字段或者多个字段进行分组,
group by 属性名 [having 条件表达式]

group by可以和group_concat()函数一起使用,该函数会把每个分组中指定字段的值显示出来。同时,group也可以和集合函数一起使用。
如果group by不与上述函数一起使用,那么查询结果就是字段取值的分组情况。

having条件表达式和where条件表达式都是用来限制显示的,但是两者作用地方不一样,where用于表或者视图,是表和视图的查询条件。having用于分组后的记录,用于选择满足条件的组

mysql> select Ssex from Student group by Ssex;
+------+
| Ssex |
+------+
||
||
+------+
2 rows in set (0.00 sec)

mysql> select Ssex,count(Ssex) from Student group by Ssex;
+------+-------------+
| Ssex | count(Ssex) |
+------+-------------+
||           3 |
||           2 |
+------+-------------+
2 rows in set (0.00 sec)

//分组查询需要显示多个结果,用逗号隔开,group_concat()这个函数用于显示此分组中
//你需要显示的项
mysql> select Ssex,group_concat(Sname) from Student group by Ssex;
+------+---------------------+
| Ssex | group_concat(Sname) |
+------+---------------------+
|| 吴兰,孙兰           |
|| 赵雷,钱电,孙风      |
+------+---------------------+
2 rows in set (0.05 sec)

//使用集合函数
mysql> select Sage,count(Sage) from Student group by Sage;
+------+-------------+
| Sage | count(Sage) |
+------+-------------+
|   20 |           2 |
|   21 |           1 |
|   18 |           1 |
|   17 |           1 |
+------+-------------+
4 rows in set (0.00 sec)

mysql> select Sage,count(Sname) from Student group by Sage;
+------+--------------+
| Sage | count(Sname) |
+------+--------------+
|   20 |            2 |
|   21 |            1 |
|   18 |            1 |
|   17 |            1 |
+------+--------------+
4 rows in set (0.00 sec)
group by 属性 having的使用
  • 注意:
    后面的having 只能使用函数或者和属性相关的判断。
//having使用函数
mysql> select Sage,count(Sname) from Student group by Sage having count(Sage)>=2;
+------+--------------+
| Sage | count(Sname) |
+------+--------------+
|   20 |            2 |
+------+--------------+
1 row in set (0.00 sec)

mysql> select Sage,count(Sname) from Student group by Sage having count(Sname)>=2;
+------+--------------+
| Sage | count(Sname) |
+------+--------------+
|   20 |            2 |
+------+--------------+
1 row in set (0.00 sec)


//使用判断
mysql> select Sage,count(Sname) from Student group by Sage having Sage>20;
+------+--------------+
| Sage | count(Sname) |
+------+--------------+
|   21 |            1 |
+------+--------------+
1 row in set (0.00 sec)
mysql> select Ssex,count(Sname) from Student group by Ssex having Ssex='女';
+------+--------------+
| Ssex | count(Sname) |
+------+--------------+
||            2 |
+------+--------------+
1 row in set (0.00 sec)
按多个字段进行分组:

group by 属性1,属性2,属性n;
在分组过程中,先按照属性1进行分组,遇到属性1的值相等的情况,在把属性1相等的值按照属性2进行分组,依此类推。

mysql> select * from SC;
+------+------+-------+
| SID  | CID  | score |
+------+------+-------+
|    1 |    1 |    80 |
|    1 |    2 |    71 |
|    1 |    3 |    87 |
|    2 |    1 |    88 |
|    2 |    2 |    70 |
|    2 |    3 |    89 |
|    3 |    1 |    68 |
|    3 |    2 |    78 |
|    3 |    3 |    87 |
|    4 |    1 |    67 |
|    4 |    2 |    58 |
|    4 |    3 |    89 |
|    5 |    1 |    56 |
|    5 |    2 |    89 |
|    6 |    3 |    38 |
+------+------+-------+
15 rows in set (0.00 sec)

mysql> select SID,CID from SC group by SID,CID;
+------+------+
| SID  | CID  |
+------+------+
|    1 |    1 |
|    1 |    2 |
|    1 |    3 |
|    2 |    1 |
|    2 |    2 |
|    2 |    3 |
|    3 |    1 |
|    3 |    2 |
|    3 |    3 |
|    4 |    1 |
|    4 |    2 |
|    4 |    3 |
|    5 |    1 |
|    5 |    2 |
|    6 |    3 |
+------+------+
15 rows in set (0.00 sec)
group by和 with rollup(归纳)一起使用

使用with rollup时,会在所有的记录的最后加一条记录,用于记录上面所有记录的总和。

//最后统计所有记录总和
mysql> select Ssex,count(Ssex) from Student group by Ssex with rollup;
+------+-------------+
| Ssex | count(Ssex) |
+------+-------------+
||           2 |
||           3 |
| NULL |           5 |
+------+-------------+
3 rows in set (0.05 sec)


group_concat和with rollup配合使用

最后一项是所有分组得到的结果。

mysql> select Ssex,group_concat(Sname) from Student group by Ssex with rollup;
+------+--------------------------+
| Ssex | group_concat(Sname)      |
+------+--------------------------+
|| 吴兰,孙兰                |
|| 赵雷,钱电,孙风           |
| NULL | 吴兰,孙兰,赵雷,钱电,孙风 |
+------+--------------------------+
3 rows in set (0.00 sec)
distinct和group by的区别:

distinct只是将重复的行从结果中去除,并且只能显示要distinct的属性。多个属性进行distinct时,只要属性组合起来和其它不同就是独一无二的。
group by在查询时先把记录按照类别分出来在查询。group by必须在查询结果中包含一个聚集函数,而distinct不用。
单纯去重来说,distinct效率是高于group by的

limit 分页查询:

limit 数据 (表示需要返回几条数据)默认从0开始。
limit 数据1(起始位置) 数据2(偏移量)
如果只有一个参数,表示不指定起始位置。
如果有两个参数,第一个表示起始位置,第二个表示偏移量。

示例:
mysql> select *  from Student limit 2;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)

mysql> select *  from Student limit 1,2;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   2 | 钱电  |   20 ||
|   3 | 孙风  |   21 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)

mysql> select *  from Student limit 0,2;
+-----+-------+------+------+
| SID | Sname | Sage | Ssex |
+-----+-------+------+------+
|   1 | 赵雷  |   20 ||
|   2 | 钱电  |   20 ||
+-----+-------+------+------+
2 rows in set (0.00 sec)

SQL提供的聚合函数操作

  • count() :计数
mysql> select count(Sage) from Student ;
+-------------+
| count(Sage) |
+-------------+
|           5 |
+-------------+
1 row in set (0.06 sec)
  • sum() :求和
mysql> select sum(Sage) from Student ;
+-----------+
| sum(Sage) |
+-----------+
|        96 |
+-----------+
1 row in set (0.00 sec)
  • max(): 最大值
mysql> select max(Sage) from Student ;
+-----------+
| max(Sage) |
+-----------+
|        21 |
+-----------+
1 row in set (0.00 sec)

  • min():最小值
mysql> select min(Sage) from Student ;
+-----------+
| min(Sage) |
+-----------+
|        17 |
+-----------+
1 row in set (0.06 sec)

  • avg():平均值
mysql> select avg(SID) from Student ;
+----------+
| avg(SID) |
+----------+
|   3.0000 |
+----------+
1 row in set (0.00 sec)
count(0)和count(1)的区别:
mysql> select count(0) from Student ;
+----------+
| count(0) |
+----------+
|        5 |
+----------+
1 row in set (0.06 sec)

mysql> select count(1) from Student ;
+----------+
| count(1) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

取别名:

属性取别名:

属性名 [as] 别名

表取别名:

表名 [as] 别名

表联合查询:

内查询:将两个表中同时存在的数据打印

表1 inner join 表2 on 表1.属性=表2.属性

示例
mysql> select * from Student inner join SC on SC.SID=Student.SID;
+-----+-------+------+------+------+------+-------+
| SID | Sname | Sage | Ssex | SID  | CID  | score |
+-----+-------+------+------+------+------+-------+
|   1 | 赵雷  |   20 ||    1 |    1 |    80 |
|   1 | 赵雷  |   20 ||    1 |    2 |    71 |
|   1 | 赵雷  |   20 ||    1 |    3 |    87 |
|   2 | 钱电  |   20 ||    2 |    1 |    88 |
|   2 | 钱电  |   20 ||    2 |    2 |    70 |
|   2 | 钱电  |   20 ||    2 |    3 |    89 |
|   3 | 孙风  |   21 ||    3 |    1 |    68 |
|   3 | 孙风  |   21 ||    3 |    2 |    78 |
|   3 | 孙风  |   21 ||    3 |    3 |    87 |
|   4 | 吴兰  |   18 ||    4 |    1 |    67 |
|   4 | 吴兰  |   18 ||    4 |    2 |    58 |
|   4 | 吴兰  |   18 ||    4 |    3 |    89 |
|   5 | 孙兰  |   17 ||    5 |    1 |    56 |
|   5 | 孙兰  |   17 ||    5 |    2 |    89 |
+-----+-------+------+------+------+------+-------+
14 rows in set (0.00 sec)


mysql> select * from Student inner join SC on SC.SID=Student.SID where Student.SID=1;
+-----+-------+------+------+------+------+-------+
| SID | Sname | Sage | Ssex | SID  | CID  | score |
+-----+-------+------+------+------+------+-------+
|   1 | 赵雷  |   20 ||    1 |    1 |    80 |
|   1 | 赵雷  |   20 ||    1 |    2 |    71 |
|   1 | 赵雷  |   20 ||    1 |    3 |    87 |
+-----+-------+------+------+------+------+-------+
3 rows in set (0.00 sec)

外查询:left|right
  • 右连接:
    将左表和右表相同属性值的结果返回,将右表存在而左表不存在的数据也返回,左表不存在的属性值为null。 左表存在右表不存的则忽略。
    表1 right join 表2 on 表1.属性=表2.属性
示例:
mysql> select * from  Student s right join SC sc on s.SID=sc.SID;
+------+-------+------+------+------+------+-------+
| SID  | Sname | Sage | Ssex | SID  | CID  | score |
+------+-------+------+------+------+------+-------+
|    1 | 赵雷  |   20 ||    1 |    1 |    80 |
|    1 | 赵雷  |   20 ||    1 |    2 |    71 |
|    1 | 赵雷  |   20 ||    1 |    3 |    87 |
|    2 | 钱电  |   20 ||    2 |    1 |    88 |
|    2 | 钱电  |   20 ||    2 |    2 |    70 |
|    2 | 钱电  |   20 ||    2 |    3 |    89 |
|    3 | 孙风  |   21 ||    3 |    1 |    68 |
|    3 | 孙风  |   21 ||    3 |    2 |    78 |
|    3 | 孙风  |   21 ||    3 |    3 |    87 |
|    4 | 吴兰  |   18 ||    4 |    1 |    67 |
|    4 | 吴兰  |   18 ||    4 |    2 |    58 |
|    4 | 吴兰  |   18 ||    4 |    3 |    89 |
|    5 | 孙兰  |   17 ||    5 |    1 |    56 |
|    5 | 孙兰  |   17 ||    5 |    2 |    89 |
| NULL | NULL  | NULL | NULL |    6 |    3 |    38 |
+------+-------+------+------+------+------+-------+
15 rows in set (0.00 sec)
  • 左连接:
    将左表和右表相同的属性值的结果返回,将左表存在而右表不存在的也返回,右表对应属性为null。将右表存在而左表不存在的则忽略掉。
    表1 left join 表2 on 表1.属性=表2.属性
示例:
mysql> select * from Student s left join SC sc on s.SID=sc.SID;
+-----+-------+------+------+------+------+-------+
| SID | Sname | Sage | Ssex | SID  | CID  | score |
+-----+-------+------+------+------+------+-------+
|   1 | 赵雷  |   20 ||    1 |    1 |    80 |
|   1 | 赵雷  |   20 ||    1 |    2 |    71 |
|   1 | 赵雷  |   20 ||    1 |    3 |    87 |
|   2 | 钱电  |   20 ||    2 |    1 |    88 |
|   2 | 钱电  |   20 ||    2 |    2 |    70 |
|   2 | 钱电  |   20 ||    2 |    3 |    89 |
|   3 | 孙风  |   21 ||    3 |    1 |    68 |
|   3 | 孙风  |   21 ||    3 |    2 |    78 |
|   3 | 孙风  |   21 ||    3 |    3 |    87 |
|   4 | 吴兰  |   18 ||    4 |    1 |    67 |
|   4 | 吴兰  |   18 ||    4 |    2 |    58 |
|   4 | 吴兰  |   18 ||    4 |    3 |    89 |
|   5 | 孙兰  |   17 ||    5 |    1 |    56 |
|   5 | 孙兰  |   17 ||    5 |    2 |    89 |
+-----+-------+------+------+------+------+-------+
14 rows in set (0.00 sec)

过滤:

不进行过滤的数据非常大,导致通过网络传输了多余的数据,从而浪费了网络带宽。因此进行使用SQL语句来过滤不必要的数据,而不是传输所有数据到客户端进行过滤。

where子句可用的操作符:
操作符 说明
= 等于
< 小于
> 大于
<>!= 不等于
<=!> 小于等于
>=!< 大于等于
between...and 两个值之间
is null 为null值

应该注意到,null与0,空字符串都不同
and和or用于连接多个过滤条件。优先处理and,当一个过滤表达式设计多个and和or时,可以使用()来决定优先级,使得优先级关系更清晰。
in 操作符用于匹配一组值,其后也可以接一个select字句,从而匹配查询得到一组值。
not操作符用于否定一个条件

猜你喜欢

转载自blog.csdn.net/Fly_Fly_Zhang/article/details/93520966