1.MySQL运算符

1.算数运算符:

加+、减-、乘*、除/、求余%

2.比较运算符:


运算符

语法

说明

=

 a=b

如果参与计算的两个操作数相等则为true,否则false

!=或者<>

a!=b或者a<>b

如果两个操作数不相等则true[1],否则false[0]

<

a<b

 如果a小于b则返回true,否则false

>

a>b

 如果a大于b则true

<=

 a<=b

小于等于

>=

 a>=b

大于等于


in/not in

in用于判断某个列的取值是否为指定的值,使用in运算符时指定的值是离散的数据,不是连续值

between/and

用于判断数据是否在指定的范围内,连续值

like/not like

主要针对字符串类型数据进行模糊查询,通配符_和%

is null/is not null

判断是否为空,为空则返回true


3.逻辑运算符



语法

说明

&&、and

a and b 、a&&b

逻辑与,如果参与计算的两个操作数都为true返回true,否则false

|| 、or

a or b、a||b

逻辑或,如果参与计算的双方,只要一方为true,则返回true,

not、!

not a 、!a

逻辑非,如果操作数为false则结果为true



建立样表 

create table if not exists tb_student(
     id bigint not null auto_increment,
     name varchar(32) not null,
     age int default 16,
     sex boolean default 1,
     dept varchar(32),
     primary key(id)
) engine=innodb default charset utf8;

insert into tb_student values(null,'张三',18,1,'软件'),
(null,'李四',16,0,'计科'),(null,'王五',19,1,'大数据'),
(null,'赵六',16,1,'软件'),(null,'田七',16,0,'计科');

2.聚集函数

计数:count统计行数    语法:count(1),count(*),count(列名)

         distinct(去除重复值)

例句:统计学生人数:

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

mysql>
mysql> select count(1),count(*),count(id) from tb_student;
+----------+----------+-----------+
| count(1) | count(*) | count(id) |
+----------+----------+-----------+
|        5 |        5 |         5 |
+----------+----------+-----------+
1 row in set (0.03 sec)

例句:统计一共有几个班级

mysql> select count(distinct dept) from tb_student;
+----------------------+
| count(distinct dept) |
+----------------------+
|                    3 |
+----------------------+
1 row in set (0.06 sec)

统计:sum求和、max最大值、min最小值、avg平均值

例句:求学生年龄和,年龄最大值,

mysql> select * from tb_student;
+----+------+------+------+--------+
| id | name | age  | sex  | dept   |
+----+------+------+------+--------+
|  1 | 张三 |   18 |    1 | 软件   |
|  2 | 李四 |   16 |    0 | 计科   |
|  3 | 王五 |   19 |    1 | 大数据 |
|  4 | 赵六 |   16 |    1 | 软件   |
|  5 | 田七 |   16 |    0 | 计科   |
+----+------+------+------+--------+
5 rows in set (0.06 sec)

mysql> select sum(age),max(age),min(age),avg(age) from tb_student;
+----------+----------+----------+----------+
| sum(age) | max(age) | min(age) | avg(age) |
+----------+----------+----------+----------+
|       85 |       19 |       16 |  17.0000 |
+----------+----------+----------+----------+
1 row in set (0.09 sec)

3.对查询结果分组

group by子句对查询结果进行分组处理,经常会使用聚集函数,分别作用于各个分组查询的结果数据,如果在select之后不在聚集函数中的列名称一定出现在group by之后,否则语法错误

having可以对分组进行条件选择

例句:统计各个班级的人数:select dept,count(1) from tb_student group by dept;

          查询班级人数大于1的班级:select dept,count(1) from tb_student group by dept having count(1)>1;

mysql> select dept,count(1) from tb_student group by dept;
+--------+----------+
| dept   | count(1) |
+--------+----------+
| 软件   |        2 |
| 计科   |        2 |
| 大数据 |        1 |
+--------+----------+
3 rows in set (0.00 sec)

mysql> select dept,count(1) from tb_student group by dept having count(1)>1;
+------+----------+
| dept | count(1) |
+------+----------+
| 软件 |        2 |
| 计科 |        2 |
+------+----------+
2 rows in set (0.00 sec)

4.排序处理

order by子句中可以针对查询结果进行排序,默认是自然序,desc倒序,asc是正序(自然序)

语法:select ... from ... order by 列名称1 [asc/desc],列名称2 [asc/desc],...先按照列名1排序,相同再按照列名2排序

例句:查询所有学生的信息按年龄从小到到排列

          select * from tb_student order by age;   select * from tb_student order by age asc;

mysql> select * from tb_student order by age;
+----+------+------+------+--------+
| id | name | age  | sex  | dept   |
+----+------+------+------+--------+
|  2 | 李四 |   16 |    0 | 计科   |
|  4 | 赵六 |   16 |    1 | 软件   |
|  5 | 田七 |   16 |    0 | 计科   |
|  1 | 张三 |   18 |    1 | 软件   |
|  3 | 王五 |   19 |    1 | 大数据 |
+----+------+------+------+--------+
5 rows in set (0.00 sec)

例句:查询所有学生的信息按年龄从小到到排列,年龄相同按id 从大到小排列

mysql> select * from tb_student order by age asc,id desc;
+----+------+------+------+--------+
| id | name | age  | sex  | dept   |
+----+------+------+------+--------+
|  5 | 田七 |   16 |    0 | 计科   |
|  4 | 赵六 |   16 |    1 | 软件   |
|  2 | 李四 |   16 |    0 | 计科   |
|  1 | 张三 |   18 |    1 | 软件   |
|  3 | 王五 |   19 |    1 | 大数据 |
+----+------+------+------+--------+
5 rows in set (0.00 sec)