MySQL的高级语言(select)
MySQL的高级语言(select)
按关键字排序
1、使用order by语句来实现排序
2、排序可针对一个或多个字段
3、ASC:升序,默认排序方式
4、DESC:降序
5、order by的语法结构
select 字段1,字段2 from 表名 order by 字段1 desc|asc,字段2 desc|asc;
6、按单字段排序
降序
mysql> select id,name,age from test order by id desc;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 4 | aaa | 23 |
| 3 | lihua | 21 |
| 2 | bbb | 100 |
| 1 | zhangsan | 20 |
+------+----------+------+
4 rows in set (0.00 sec)
升序
mysql> select id,name,age from test order by id asc;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | bbb | 100 |
| 3 | lihua | 21 |
| 4 | aaa | 23 |
+------+----------+------+
对结果进行分组
使用group by语句来实现分组
通常结合聚合函数一起使用
可以按一个或多个字段对结果进行分组
group by的语法结构
mysql> select count(name),age from test where age>=23 group by age;## 在test表中查找年纪大于等于23岁的 出现的次数
+-------------+------+
| count(name) | age |
+-------------+------+
| 1 | 23 |
| 1 | 100 |
+-------------+------+
2 rows in set (0.00 sec)
增加按分数降序或者升序
mysql> select count(name),age from test where age>=23 group by age order by age desc;
+-------------+------+
| count(name) | age |
+-------------+------+
| 1 | 100 |
| 1 | 23 |
+-------------+------+
2 rows in set (0.00 sec)
增加按分数降序
mysql> select count(name),age from test where age>=23 group by age order by age asc;
+-------------+------+
| count(name) | age |
+-------------+------+
| 1 | 23 |
| 1 | 100 |
+-------------+------+
2 rows in set (0.00 sec)
限制结果条目
1、只返回select查询结果的第一行或前几行
2、使用limit语句限制条目
3、limit语法结构
mysql> select * from test limit 3; ## 显示三行数据
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | bbb | 100 |
| 3 | lihua | 21 |
+------+----------+------+
mysql> select * from test limit 2,3; ## 从第二行开始显示后三行的数据(不包括第二行)
+------+-------+------+
| id | name | age |
+------+-------+------+
| 3 | lihua | 21 |
| 4 | aaa | 23 |
| 5 | ccc | 24 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from test order by age desc limit 3; 显示age 最大的三行
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | bbb | 100 |
| 5 | ccc | 24 |
| 4 | aaa | 23 |
+------+------+------+
mysql> select * from test order by age asc limit 3;## 显示年纪最小的三行
+------+----------+------+
| id | name | age |
+------+----------+------+
| 6 | ddd | 17 |
| 1 | zhangsan | 20 |
| 3 | lihua | 21 |
+------+----------+------+
3 rows in set (0.00 sec)
设置别名
1、使用as语句设置别名,关键字as可省略
2、设置别名时,保证不能与库中其他表或字段名称冲突
3、别名的语法结构
字段别名:
select 字段 as 别名 from 表名;
表的别名:
select 字段 from 表名 as 别名;
mysql> select id as num from test; ## 设置数据表 test 的id的别名为num 并且显示出来
+------+
| num |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+------+
6 rows in set (0.00 sec)
通配符
1、用于替换字符串中的部分字符
2、通常配合like一起使用,并协同where完成查询
3、常用通配符
mysql> select * from test where name like 'z%'; ## 匹配z开头的name
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
+------+----------+------+
1 row in set (0.00 sec)
mysql> select * from test where name like 'zhang___'; ## 几个_ 就是匹配zhang后面的三个未知字符
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
+------+----------+------+
mysql> select * from test where name like '__a%'; ## a前面两个未知字符开头的;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 4 | aaa | 23 |
+------+----------+------+
2 rows in set (0.00 sec)
子查询
1、也称作内查询或者嵌套查询
2、先于主查询被执行,其结果将作为外层主查询的条件
3、在增删改查中都可以使用子查询
4、支持多层嵌套
5、in语句用来判断某个值是否在给定的结果集中
mysql> select id,name from test where id in(1,2); ## 看id,name,只看id为1和2的值
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 2 | bbb |
+------+----------+
2 rows in set (0.00 sec)
mysql> select id,name from test where id in(select id from test where id>2);# 查看id ,name 只看id大于2 的
+------+-------+
| id | name |
+------+-------+
| 3 | lihua |
| 4 | aaa |
| 5 | ccc |
| 6 | ddd |
+------+-------+
4 rows in set (0.00 sec)
mysql> select id ,name from test where id in (select id from test where name='zhangsan'); #先查找test的zhangsan的id 为1 并且显示出来 在再test表中id 为1 的 显示出来+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 1 | zhangwu |
+------+----------+
2 rows in set (0.00 sec)
mysql> select id ,name from test where id != (select id from test where name='zhangsan'); # 不等于
+------+-------+
| id | name |
+------+-------+
| 2 | bbb |
| 3 | lihua |
| 4 | aaa |
| 5 | ccc |
| 6 | ddd |
+------+-------+
5 rows in set (0.00 sec)
mysql> select id ,name from test where id <> (select id from test where name='zhangsan'); # <>等于!=
+------+-------+
| id | name |
+------+-------+
| 2 | bbb |
| 3 | lihua |
| 4 | aaa |
| 5 | ccc |
| 6 | ddd |
+------+-------+
5 rows in set (0.00 sec)
多层嵌套
mysql> select id,name from test where id in (select id from (select id from test where name='zhangsan') test); #最内层的值传递给上层,依次上传
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 1 | zhaosan |
+------+----------+
2 rows in set (0.00 sec)
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 1 | zhangwu |
+------+----------+
exists的用法
mysql> select * from test where exists (select * from test where age=21); ##exists 与if 类似 后面的语句执行成功在执行前面的语句 test表中有 age为21 的 再展示表中所有信息;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | bbb | 100 |
| 3 | lihua | 21 |
| 4 | aaa | 23 |
| 5 | ccc | 24 |
| 6 | ddd | 17 |
| 1 | zhangwu | 33 |
NULL值
null:真空(什么都没有)
‘’:空气(还有空气)
1、表示缺失的值
2、与数字0或者空白(spaces)是不同的
3、使用is null或is not null进行判断
4、null值和空值(’’)的区别
空值长度为0,不占空间;null值的长度为null,占用空间
is null无法判断空值
空值使用“=”或者“<>”来处理
count()计算时,null会忽略,空值会加入计算
mysql> insert into test (id ,name) values(7,eee);
mysql> select * from test;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
| 1 | aaa | 96 |
| 2 | bbb | 100 |
| 7 | eee | NULL |
| 1 | aaa | 96 |
+------+--------+------+
5 rows in set (0.00 sec)
mysql> select count(age) from test;
+------------+
| count(age) |
+------------+
| 4 |
+------------+
mysql> select * from test where age is null;## 查询age为null的数据;
+------+------+------+
| id | name | age |
+------+------+------+
| 7 | eee | NULL |
+------+------+------+
1 row in set (0.00 sec)
mysql> select * from test where age is not null;# 查询age不为null的数据;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
| 1 | aaa | 96 |
| 2 | bbb | 100 |
| 1 | aaa | 96 |
正则表达式
1、根据指定的匹配模式匹配记录中符合要求的特殊字符
2、使用regexp关键字指定匹配模式
3、常用匹配模式
^ 匹配文本的开始字符
$ 匹配文本的结束字符
. 匹配任何单个字符
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
字符串 匹配包含指定的字符串
p1lp2 匹配p1或p2
[…] 匹配字符集合中任一字符
[^…] 匹配不在括号中的任一字符
{
n} 匹配前面的字符串n次
{
n,m} 匹配前面的字符串至少n次,之多m次
1、以特定字符串开头的记录
mysql> select * from test where name regexp '^w';## 以w开头的name字段的数据
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
+------+--------+------+
1 row in set (0.00 sec)
2、以特定字符串结尾的记录
mysql> select * from test where name regexp 'u$';## 以u结尾的name字段数据;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
+------+--------+------+
1 row in set (0.01 sec)
mysql> select * from test where name regexp 'wang';## 指定字符串;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
+------+--------+------+
1 row in set (0.00 sec)
mysql> select * from test where name regexp 'wang..'; ## name字段 wang后面任意两个字符串;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
+------+--------+------+
1 row in set (0.00 sec)
mysql> select * from test where name regexp 'wang|aaa'; ##wang 或者 aaa;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 2 | wangwu | 19 |
| 1 | aaa | 96 |
| 1 | aaa | 96 |
+------+--------+------+
3 rows in set (0.00 sec)
运算符
用于对记录中的字段值进行运算
MySQL 的运算符共有四种,分别是:算术运算符、比较运算符、逻辑运算符和位运算符
- 算数运算符
运算符 | 描述 |
---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取余数 |
- 以select命令来实现最基础的加减乘除运算
mysql> select 1+2,1+3,3-1,2-3,2*3,4/2,6%5;
+-----+-----+-----+-----+-----+--------+------+
| 1+2 | 1+3 | 3-1 | 2-3 | 2*3 | 4/2 | 6%5 |
+-----+-----+-----+-----+-----+--------+------+
| 3 | 4 | 2 | -1 | 6 | 2.0000 | 1 |
+-----+-----+-----+-----+-----+--------+------+
1 row in set (0.00 sec)
注意:某些字符串类型的字段存储的数字型字符串,这些字段在进行算术运算时将会被自动转换为数字的值。如果字符串的开始部分是数字,在转换时将被转换为这个数字。如果是既包含字符又包含数字得的混合字符串,无法转换为数字时,将被转换为 0。
mysql> select 2*'8int';
+----------+
| 2*'8int' |
+----------+
| 16 |
+----------+
mysql> select 2*'int8';
+----------+
| 2*'int8' |
+----------+
| 0 |
+----------+
运算符 | 描述 |
---|---|
= | 等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
!= 或者<> | 不等于 |
is null | 判断是否一个值是否为null |
is not null | 判断一个值是否不为null |
between and | 两者之间 |
in | 在集合中 |
like | 通配符匹配 |
greatest | 两个或者多个参数时返回最大值 |
least | 两个或多个参数时返回最小值 |
regext | 正则表达式 |
等于运算符
比较规则:
如果两者都是整数,则按照整数值进行比较。
如果一个整数一个字符串,则会自动将字符串转换为数字,再进行比较。
如果两者都是字符串,则按照字符串进行比较。
如果两者中至少有一个值是 NULL,则比较的结果是 NULL。
mysql> select 1=1,1=2,1='1','a'='a','a'='b','a'=null;
+-----+-----+-------+---------+---------+----------+
| 1=1 | 1=2 | 1='1' | 'a'='a' | 'a'='b' | 'a'=null |
+-----+-----+-------+---------+---------+----------+
| 1 | 0 | 1 | 1 | 0 | NULL |
+-----+-----+-------+---------+---------+----------+
不等于运算符
不等于号有两种写法,分别是<>或者!=,用于针对数字、字符串和表达式不相等的比较。如果不相等则返回 1,如果相等则返回 0。
需要注意的是不等于运算符不能用于判断 NULL。
mysql> select 'a'!='ab','ab'<>'abc',1!=2,1<>2,1<>null;
+-----------+-------------+------+------+---------+
| 'a'!='ab' | 'ab'<>'abc' | 1!=2 | 1<>2 | 1<>null |
+-----------+-------------+------+------+---------+
| 1 | 1 | 1 | 1 | NULL |
+-----------+-------------+------+------+---------+
1 row in set (0.00 sec)
大于、大于等于、小于、小于等于运算符
注意:都不能用于判断NULL。
mysql> select 1>2,1>=2,1<2,1<=2,1>null;
+-----+------+-----+------+--------+
| 1>2 | 1>=2 | 1<2 | 1<=2 | 1>null |
+-----+------+-----+------+--------+
| 0 | 0 | 1 | 1 | NULL |
+-----+------+-----+------+--------+
1 row in set (0.00 sec)
IS NULL、IS NOT NULL
IS NULL 判断一个值是否为 NULL,如果为 NULL 返回 1,否则返回 0。
IS NOT NULL 判断一个值是否不为 NULL,如果不为 NULL 返回 1,否则返回 0。
mysql> mysql> select 'a' is null, 'a' is not null, null is null, null is not null;
+-------------+-----------------+--------------+------------------+
| 'a' is null | 'a' is not null | null is null | null is not null |
+-------------+-----------------+--------------+------------------+
| 0 | 1 | 1 | 0 |
+-------------+-----------------+--------------+------------------+
1 row in set (0.00 sec)
between and
用于判断一个值是否落在某两个值之间
例如,判断某数字是否在另外两个数字之间,也可以判断某英文字母是否在另外两个字母之间
mysql> sysql> select 2 between 1 and 3, 'd' between 'a' and 'c';
+--------------------------+-------------------------+
| 2 between 1 and 31 and 3 | 'd' between 'a' and 'c' |
+--------------------------+-------------------------+
| 1 | 0 |
+--------------------------+-------------------------+
1 row in set (0.00 sec)
least、greatest
LEAST:当有两个或者多个参数时,返回其中的最小值。如果其中一个值为 NULL,则返回结果就为 NULL。
GREATEST:当有两个或者多个参数时,返回其中的最大值。如果其中一个值为 NULL, 则返回结果就为 NULL。
mysql> select least(1,2,3), greatest(1,2,3);
+--------------+-----------------+
| least(1,2,3) | greatest(1,2,3) |
+--------------+-----------------+
| 1 | 3 |
mysql> select least('a','b','c'), greatest('a','b','c');
+--------------------+-----------------------+
| least('a','b','c') | greatest('a','b','c') |
+--------------------+-----------------------+
| a | c |
mysql> select least(1,2,null), greatest(1,2,null);
+-----------------+--------------------+
| least(1,2,null) | greatest(1,2,null) |
+-----------------+--------------------+
| NULL | NULL |
in、not in
IN 判断一个值是否在对应的列表中,如果是返回 1,否则返回 0。
NOT IN 判断一个值是否不在对应的列表中,如果不是返回 1,否则返回 0。
mysql> select 1 in (1,2,3), 'a' not in (1,2,3), 1 in ('a','b','c');
+--------------+--------------------+--------------------+
| 1 in (1,2,3) | 'a' not in (1,2,3) | 1 in ('a','b','c') |
+--------------+--------------------+--------------------+
| 1 | 1 | 0 |
+--------------+--------------------+--------------------+
1 row in set, 4 warnings (0.00 sec)
like、not like
LIKE 用来匹配字符串,如果匹配成功则返回 1,反之返回 0;NOT LIKE 正好跟 LIKE 相反。
LIKE 支持两种通配符:’%’ 用于匹配任意数目的字符,而’_’只能匹配一个字符。
mysql> select 'abc' like 'a%', 'abc' like 'ab_', 'abc' n
+-----------------+------------------+------------------
| 'abc' like 'a%' | 'abc' like 'ab_' | 'abc' not like '_
+-----------------+------------------+------------------
| 1 | 1 |
+-----------------+------------------+------------------
1 row in set (0.00 sec)
逻辑运算符
又被称为布尔运算符
用来判断表达式的真假
NOT →逻辑非
AND或&&→逻辑与
OR或|→逻辑或
XOR→逻辑异或
逻辑非
逻辑非将跟在它后面的逻辑测试取反,把真变为假,把假变为真。
如果 NOT 后面的操作数为 0 时,所得值为 1;如果操作数为非 0 时,所得值为 0;如果操作数为 NULL 时,所得值为 NULL。
注意:非0值都是1
mysql> select not 2, ! 0, not (1-1), ! null;
+-------+-----+-----------+--------+
| not 2 | ! 0 | not (1-1) | ! null |
+-------+-----+-----------+--------+
| 0 | 1 | 1 | NULL |
+-------+-----+-----------+--------+
1 row in set (0.00 sec)
逻辑与
如果所有值都是真返回 1,否则返回 0。
mysql> select 1 and 2, 2 && 2, 1 and null, 1 and 0, 0 && null;
+---------+--------+------------+---------+-----------+
| 1 and 2 | 2 && 2 | 1 and null | 1 and 0 | 0 && null |
+---------+--------+------------+---------+-----------+
| 1 | 1 | NULL | 0 | 0 |
+---------+--------+------------+---------+-----------+
1 row in set (0.00 sec)
逻辑或(最好用or)
逻辑或表示包含的操作数,任意一个为非零值并且不是 NULL 值时,返回 1,否则返回0。
mysql> select 1 or 1, 1 or 0, 1 or null, 0 or null, 0 or 0;
+--------+--------+-----------+-----------+--------+
| 1 or 1 | 1 or 0 | 1 or null | 0 or null | 0 or 0 |
+--------+--------+-----------+-----------+--------+
| 1 | 1 | 1 | NULL | 0 |
+--------+--------+-----------+-----------+--------+
1 row in set (0.00 sec)
逻辑异或
两个非 NULL 值的操作数,如果两者都是 0 或者都是非 0,则返回 0;如果一个为 0, 另一个为非 0,则返回结果为 1;
当任意一个值为 NULL 时,返回值为 NULL。
mysql> select 0 xor 1, 1 xor 1, 0 xor null, 0 xor 0;
+---------+---------+------------+---------+
| 0 xor 1 | 1 xor 1 | 0 xor null | 0 xor 0 |
+---------+---------+------------+---------+
| 1 | 0 | NULL | 0 |
+---------+---------+------------+---------+
1 row in set (0.00 sec)
连接查询
通常都是将来自两个或多个表的行结合起来,基于这些表之间的共同字段,进行数据的拼接。
要先确定一个主表作为结果集,然后将其他表的行有选择性的连接到选定的主表结果集上。
使用较多的连接查询包括:内连接、左连接和右连接
内连接
在from子句中使用关键字 inner join 来连接多张表,并使用 on子句设置连接条件。
mysql> insert into age values(96,'老年'),(19,'成年');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from age;
+------+--------------+
| age | 是否老年 |
+------+--------------+
| 96 | 老年 |
| 19 | 成年 |
+------+--------------
mysql> select test.name,age.是否老年 from test inner join age on test.age=age.age;
+--------+--------------+
| name | 是否老年 |
+--------+--------------+
| wangwu | 成年 |
| aaa | 老年 |
| aaa | 老年 |
+--------+--------------+
内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用关键字 JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表。
外连接
左连接,主表在左边,主表内容会全部显示出来,在从表中没匹配到的以NULL显示出来
mysql> select test.name, age.是否老年 from test left join age on test.age=age.age;
+--------+--------------+
| name | 是否老年 |
+--------+--------------+
| aaa | 老年 |
| aaa | 老年 |
| wangwu | 成年 |
| bbb | NULL |
| eee | NULL |
+--------+--------------+
右连接,主表在右边,主表内容会全部显示出来,在从表中没匹配到的以NULL显示出来
mysql> select test.name, age.是否老年 from test right join age on test.age=age.age;
+--------+--------------+
| name | 是否老年 |
+--------+--------------+
| wangwu | 成年 |
| aaa | 老年 |
| aaa | 老年 |
+--------+--------------+