MySQL之查询

时间:2020-03-27
本文章向大家介绍MySQL之查询,主要包括MySQL之查询使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

SQL语句

环境准备

----------------------------基本查询环境----------------------------
-- 涉及到的insert语句于下篇文章详细解释
create table employee(
    id int primary key,
    e_no varchar(20) not null,
    name varchar(20),
    age int,
    sex varchar(10),
    address varchar(255)
    ) charset=utf8mb4; -- 这里需要指定编码为utfmb4类型,lantin无法插入中文

insert into employee
	(id, name, age, sex, address, e_no)
	values
	(1, '刘备', 26, '男', '西蜀', 1001), 
	(2, '孙权', 15, '男', '东吴', 1001),
	(3, '曹操', 25, '男', '许昌', 1002),
	(4, '大乔', 12, '女', '东吴', 1004);

----------------------------连接查询环境----------------------------
-- 创建主表
create table department(
    e_no int primary key,
    d_name varchar(10)
    )engine=InnoDB default charset=utf8mb4;
-- 创建从表
create table employee(
    id int primary key,
    e_no int,
    name varchar(10),
    age int,
    sex varchar(10),
    address varchar(255)
    )engine=InnoDB default charset=utf8mb4;
-- 添加外键
alter table employee add foreign key FK_DEPART_EMP(e_no) references department(e_no);
-- 插入数据
insert into department values
	(1001, '研发部'),
	(1002, '产品部'),
	(1003, '销售部');
insert into employee(id, name, age, sex, address, e_no) values
	(1, '刘备', 26, '男', '西蜀', 1001), 
	(2, '孙权', 15, '男', '东吴', 1001),
	(3, '曹操', 25, '男', '许昌', 1002),
	(4, '大乔', 12, '女', '东吴', ''),
	(5, '孔明', 15, '男', NULL, 1003);

基本的SQL语句

语法:

select 属性列表
	from 表名(视图)列表
	[where 条件表达式]
	[group by 属性名 [having 条件表达式 2]]
	[order by 属性2 [asc|desc]]
mysql> select * from employee;
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1003 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  4 | 1004 | 大乔   |   12 | 女   | 东吴    |
+----+------+--------+------+------+---------+
4 rows in set (0.00 sec)

条件+排序

select id,name,age,sex,address from employee
	where age < 26
	order by e_no desc;
/*
+----+--------+------+------+---------+
| id | name   | age  | sex  | address |
+----+--------+------+------+---------+
|  2 | 孙权   |   15 | 男   | 东吴    |
|  3 | 曹操   |   25 | 男   | 许昌    |
|  4 | 大乔   |   12 | 女   | 东吴    |
+----+--------+------+------+---------+
*/

select id, e_no, name, age, sex, address from employee
	where age < 26
	order by e_no desc;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
+----+------+--------+------+------+---------+
*/

字段查询

查询所有字段

select * from employee;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  4 | 1004 | 大乔   |   12 | 女   | 东吴    |
+----+------+--------+------+------+---------+
*/

尽可能的使用字段查询,而不是用*,这会大大影响查询效率

查询指定字段

可根据需要指定字段,同事字段的顺序也可以改变

select name, sex, age from employee;
/*
+--------+------+------+
| name   | sex  | age  |
+--------+------+------+
| 刘备   | 男   |   26 |
| 孙权   | 男   |   15 |
| 曹操   | 男   |   25 |
| 大乔   | 女   |   12 |
+--------+------+------+
*/

select * from employee where e_no = 1001;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
+----+------+--------+------+------+---------+
*/

select * from employee where e_no = 1003;
/*
Empty set (0.00 sec)
*/

如果查询不到则返回empty,为空。

查询条件 符号或关键字
比较 =、<、<=、>、>=、!=、<>、!>、!<
指定范围 between...and...、not...between...and
指定集合 in、not in
匹配字符 like、not like
是否为空 is null、is not null
多个查询条件 and、or

查询指定记录

语法:[not] in(元素1, 元素2, 元素3, ... , 元素n)

select * from employee where e_no in (1002, 1004);
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  4 | 1004 | 大乔   |   12 | 女   | 东吴    |
+----+------+--------+------+------+---------+
*/

select name,age,sex from employee where name in ("刘备", "曹操");
/*
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| 刘备   |   26 | 男   |
| 曹操   |   25 | 男   |
+--------+------+------+
*/

查询的是记录包含in里面字段的记录

区间查询

语法:select 要查询的字段 from 要查询的表名 where 限制条件的字段 between 范围 and 范围

select name,age,sex from employee where age between 15 and 20;
/*
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| 孙权   |   15 | 男   |
+--------+------+------+
*/

select name,age,sex from employee where age not between 15 and 20;
/*
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| 刘备   |   26 | 男   |
| 曹操   |   25 | 男   |
| 大乔   |   12 | 女   |
+--------+------+------+
*/

模糊匹配查询

语法:select 要查询的字段 from 要查询的表名 where 限制条件的字段 like '限制条件'

匹配通配符:

  • %:代表任意长度的字符串,可以是一个,也可以是多个,长度可以为0

    a%b 表示以字母a开头,以字母c结尾,中间可以为任意字符,可以是abc、ac、adfgc

  • _:只能表示单个字符

    a%b 表示以字母a开头,以字母c结尾,中间只能为一个字符,可以是abc、adc、afc

select id,name,age,address from employee where address like '%东%';
/*
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
|  2 | 孙权   |   15 | 东吴    |
|  4 | 大乔   |   12 | 东吴    |
+----+--------+------+---------+
*/

select id,name,age,address from employee where address not like '_吴';
/*
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
|  1 | 刘备   |   26 | 西蜀    |
|  3 | 曹操   |   25 | 许昌    |
+----+--------+------+---------+
*/

查询空值

执行如下SQL以保证实验的准确性

insert into employee values(5, 1003, '孔明', 15, '男', NULL);
update employee set address = '' where id = 4;
select name,age from employee where address is null;
/*
+--------+------+
| name   | age  |
+--------+------+
| 孔明   |   15 |
+--------+------+
*/

select name,age from employee where address is not null;
/*
+--------+------+
| name   | age  |
+--------+------+
| 刘备   |   26 |
| 孙权   |   15 |
| 曹操   |   25 |
| 大乔   |   12 |
+--------+------+
*/

空值:

  • 从未填写过数据,值为空
  • 填写过数据后删掉的,值为null
select * from employee where address = '';
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   |         |
+----+------+--------+------+------+---------+
*/

select * from emnployee where address is null;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

条件查询

AND的多条件查询

可以使用and连接N个条件,只要有一个不满足,则此纪录将会被排除

select * from employee where age between 15 and 25 and address like '%东%';
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
+----+------+--------+------+------+---------+
*/

OR的多条件查询

可以使用or连接N个条件,只有所有的条件不满足时,此纪录才会被排除

select * from employee where age between 15 and 25 or address like '%东%';
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

select * from employee where age between 12 and 25 or address like '东_';
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  4 | 1004 | 大乔   |   12 | 女   |         |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

查询结果处理

对于查询出来的结果进行去重或者排序

结果去重

select distinct e_noi from employee;
/*
+------+
| e_no |
+------+
| 1001 |
| 1002 |
| 1004 |
| 1003 |
+------+
*/

结果升序

select * from employee order by age asc;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   |         |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
+----+------+--------+------+------+---------+
*/

select * from employee order by age;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   |         |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
+----+------+--------+------+------+---------+
*/

这里的排序字段,可以指定多个,就是说,先按照age排序,遇到年龄相同的则以e_no排序

select * from employee order by age,e_no;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   |         |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
+----+------+--------+------+------+---------+
*/

注意:order by默认升序

结果降序

select name,e_no,age from employee order by age desc;
/*
+--------+------+------+
| name   | e_no | age  |
+--------+------+------+
| 刘备   | 1001 |   26 |
| 曹操   | 1002 |   25 |
| 孙权   | 1001 |   15 |
| 孔明   | 1003 |   15 |
| 大乔   | 1004 |   12 |
+--------+------+------+
*/

分组查询

可以配合聚合函数一起使用,也可以普通的单独使用,用途非常之多

单独使用GROUP

select * from employee group by sex;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  4 | 1004 | 大乔   |   12 | 女   |         |
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
+----+------+--------+------+------+---------+
*/

这里执行报如下错的可以执行

set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

group_concat()结合

select sex,group_concat(name) from employee where group by sex;
/*
+------+-----------------------------+
| sex  | group_concat(name)          |
+------+-----------------------------+
| 女   | 大乔                        |
| 男   | 刘备,孙权,曹操,孔明         |
+------+-----------------------------+
*/

与聚合函数结合使用

select sex,group_concat(sex) from employee group by sex;
/*
+------+-------------------+
| sex  | group_concat(sex) |
+------+-------------------+
| 女   | 女                |
| 男   | 男,男,男,男       |
+------+-------------------+
*/

在后边会详细介绍使用聚合函数,传送门

having结合使用

where:对表和视图进行条件限制

having:分组后的信息筛选

select sex,count(sex) from employee group by sex having count(sex) > 3;
/*
+------+------------+
| sex  | count(sex) |
+------+------------+
| 男   |          4 |
+------+------------+
*/

多个字段分组

select * from employee group by e_no,sex;
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
|  4 | 1004 | 大乔   |   12 | 女   |         |
+----+------+--------+------+------+---------+
*/

with rollup结合使用

会加上一条记录,而该记录是查询分组后记录的总和

select sex,group_concat(name) from employee group by sex with rollup;
/*
+------+------------------------------------+
| sex  | group_concat(name)                 |
+------+------------------------------------+
| 女   | 大乔                               |
| 男   | 刘备,孙权,曹操,孔明                |
| NULL | 大乔,刘备,孙权,曹操,孔明           |
+------+------------------------------------+
*/

select sex,count(sex) from employee group by sex with rollup;
/*
+------+------------+
| sex  | count(sex) |
+------+------------+
| 女   |          1 |
| 男   |          4 |
| NULL |          5 |
+------+------------+
*/

分页查询

LIMIT

限制查询结果数量,

  1. 不指定初始化位置,只查看限制的条数

    select name,age from employee limit 2;
    /*
    +--------+------+
    | name   | age  |
    +--------+------+
    | 刘备   |   26 |
    | 孙权   |   15 |
    +--------+------+
    */
    
    select name,age from employee limit 10;
    /*
    +--------+------+
    | name   | age  |
    +--------+------+
    | 刘备   |   26 |
    | 孙权   |   15 |
    | 曹操   |   25 |
    | 大乔   |   12 |
    | 孔明   |   15 |
    +--------+------+
    */
    

    limit后边的数字表示限制的条数,若条数小于总记录,则显示限制的数量,若大于总记录数,则显示全部

  2. 指定初始化位置

    select name,age from employee limit 0,3;
    /*
    +--------+------+
    | name   | age  |
    +--------+------+
    | 刘备   |   26 |
    | 孙权   |   15 |
    | 曹操   |   25 |
    +--------+------+
    */
    

    这种和不指定效果一样,因为不指定时默认从一条数据开始,也就是0,第二条就是2,以此类推。limit也可用作分页

    select name,age from employee limit 1,3;
    /*
    +--------+------+
    | name   | age  |
    +--------+------+
    | 孙权   |   15 |
    | 曹操   |   25 |
    | 大乔   |   12 |
    +--------+------+
    */
    

聚合函数

COUNT

统计数量

select count(id) from employee;
/*
+-----------+
| count(id) |
+-----------+
|         5 |
+-----------+
*/

统计时不推荐使用count(*)也就是统计所有的字段,最好指定一个唯一性的字段,例如主键

SUM

求和,将指定记录做求和

select sum(age) from employee;
/*
+----------+
| sum(age) |
+----------+
|       93 |
+----------+
*/

SUM只能对于数值类型的记录进行求和,如果累加字符,则结果始终显示0

AVG

求平均值

select avg(age) from employee;
/*
+----------+
| avg(age) |
+----------+
|  18.6000 |
+----------+
*/

MAX

求最大值

select max(age) from employee;
/*
+----------+
| max(age) |
+----------+
|       26 |
+----------+
*/

MIN

select min(age) from employee;
/*
+----------+
| max(age) |
+----------+
|       12 |
+----------+
*/

连接查询

请确保以上的环境准备

内链接

内链接可以使用where或者inner join ... on实现,其效果是一样的

select id,name,age,sex,address,d_name from employee,department where employee.e_no = department.e_no;
/*
+----+--------+------+------+---------+-----------+
| id | name   | age  | sex  | address | d_name    |
+----+--------+------+------+---------+-----------+
|  1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|  2 | 孙权   |   15 | 男   | 东吴    | 研发部    |
|  3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
|  5 | 孔明   |   15 | 男   | NULL    | 销售部    |
+----+--------+------+------+---------+-----------+
*/
select id,name,age,sex,address,d_name from employee inner join department on employee.e_no = department.e_no;
/*
+----+--------+------+------+---------+-----------+
| id | name   | age  | sex  | address | d_name    |
+----+--------+------+------+---------+-----------+
|  1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|  2 | 孙权   |   15 | 男   | 东吴    | 研发部    |
|  3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
|  5 | 孔明   |   15 | 男   | NULL    | 销售部    |
+----+--------+------+------+---------+-----------+
*/

内链接完全匹配,A表有则B表必须有才能被查出来,若一方没有,则记录不会被查出来

外连接

如果两张表的记录不是一一对应的,则选择性显示

左外连接

select id,name,age,sex,address,d_name from employee left join department on employee.e_no = department.e_no;
/*
+----+--------+------+------+---------+-----------+
| id | name   | age  | sex  | address | d_name    |
+----+--------+------+------+---------+-----------+
|  1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|  2 | 孙权   |   15 | 男   | 东吴    | 研发部    |
|  3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
|  5 | 孔明   |   15 | 男   | NULL    | 销售部    |
|  4 | 大乔   |   12 | 女   | 东吴    | NULL      |
+----+--------+------+------+---------+-----------+
*/

指的是以左边的表为准,但是上边的大乔是没有对应的部门的,所以就以空值显示

右外连接

select id,name,age,sex,address,d_name from employee right join department on employee.e_no = department.e_no;
/*
+------+--------+------+------+---------+-----------+
| id   | name   | age  | sex  | address | d_name    |
+------+--------+------+------+---------+-----------+
|    1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|    2 | 孙权   |   15 | 男   | 东吴    | 研发部    |
|    3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
|    5 | 孔明   |   15 | 男   | NULL    | 销售部    |
+------+--------+------+------+---------+-----------+
*/

类似于做连接查询,以右边的表为主表,显示右表中的所有信息

复合查询

select id,name,age,sex,address,d_name from employee right join department on employee.e_no = department.e_no where age >= 16;
/*
+------+--------+------+------+---------+-----------+
| id   | name   | age  | sex  | address | d_name    |
+------+--------+------+------+---------+-----------+
|    1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|    3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
+------+--------+------+------+---------+-----------+
*/

在匹配之后再次限制条件,类似于group by之后的having

子查询

将一个查询语句嵌套在另一个查询语句中。内层查询语句的查询结果可以作为外层查询提供查询条件

IN关键字

-- 查询研发部的所有人的信息
select * from employee where e_no in (select e_no from department where d_name = '研发部');
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
+----+------+--------+------+------+---------+
*/

-- 查询不属于研发部的人的信息
select * from employee where e_no not in (select e_no from department where d_name = '研发部');
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

带比较运算符

-- 查询年龄是15岁的人在哪个部门
select e_no,name,d_name from department where e_no = (select e_no from employee where age = 25);
/*
+------+-----------+
| e_no | d_name    |
+------+-----------+
| 1002 | 产品部    |
+------+-----------+
*/

在使用比较运算符的时候,子查询的结果只能为1,不能为N

EXISTS关键字

-- 查询存在1002的员工
select * from employee where exists (select * from department where e_no = 1001);
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  4 | NULL | 大乔   |   12 | 女   | 东吴    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

-- 查询存在1004的员工
select * from employee where exists (select * from department where e_no = 1004);
/*
Empty set (0.00 sec)
*/

如果存在括号里面的条件则查询,如果不存在,直接不查询,也可以使用not

ANY关键字

满足子查询中的其一即可

select * from employee where e_no >= any (select e_no from department);
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  1 | 1001 | 刘备   |   26 | 男   | 西蜀    |
|  2 | 1001 | 孙权   |   15 | 男   | 东吴    |
|  3 | 1002 | 曹操   |   25 | 男   | 许昌    |
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

ALL关键字

满足子查询中的所有

select * from employee where e_no >= all (select e_no from department);
/*
+----+------+--------+------+------+---------+
| id | e_no | name   | age  | sex  | address |
+----+------+--------+------+------+---------+
|  5 | 1003 | 孔明   |   15 | 男   | NULL    |
+----+------+--------+------+------+---------+
*/

合并查询结果

union

select e_no from employee union select e_no from department;
/*
+------+
| e_no |
+------+
| NULL |
| 1001 |
| 1002 |
| 1003 |
+------+
*/

可以用于去重

union all

select e_no from employee union all select e_no from department;
/*
+------+
| e_no |
+------+
| NULL |
| 1001 |
| 1001 |
| 1002 |
| 1003 |
| 1001 |
| 1002 |
| 1003 |
+------+
*/

只是单纯的合并查询结果,没有去重的功效

别名

为表和字段取别名

为表取别名

select id,name,age,sex,address,d_name from employee as emp,department as dept where emp.e_no = dept.e_no;
/*
+----+--------+------+------+---------+-----------+
| id | name   | age  | sex  | address | d_name    |
+----+--------+------+------+---------+-----------+
|  1 | 刘备   |   26 | 男   | 西蜀    | 研发部    |
|  2 | 孙权   |   15 | 男   | 东吴    | 研发部    |
|  3 | 曹操   |   25 | 男   | 许昌    | 产品部    |
|  5 | 孔明   |   15 | 男   | NULL    | 销售部    |
+----+--------+------+------+---------+-----------+
*/

为字段取别名

select id as ID,name as '姓名',age as '年龄',sex as '性别',address as '地址',d_name as '部门名称' from employee as emp,department as dept where emp.e_no = dept.e_no;
/*
+----+--------+--------+--------+--------+--------------+
| ID | 姓名   | 年龄   | 性别   | 地址   | 部门名称     |
+----+--------+--------+--------+--------+--------------+
|  1 | 刘备   |     26 | 男     | 西蜀   | 研发部       |
|  2 | 孙权   |     15 | 男     | 东吴   | 研发部       |
|  3 | 曹操   |     25 | 男     | 许昌   | 产品部       |
|  5 | 孔明   |     15 | 男     | NULL   | 销售部       |
+----+--------+--------+--------+--------+--------------+
*/

select count(*) as '员工个数' from employee;
/*
+--------------+
| 员工个数     |
+--------------+
|            5 |
+--------------+
*/

注意:在取了别名之后,是不允许再次作为查询条件来查询

正则表达式查询

语法:属性名 regexp (regular expression) 匹配模式

正则表达式的模式字符 含义
^ 匹配字符串开始的部分
$ 匹配字符串结束的部分
. 代表字符串的任意一个字符,包含回车和换行
[字符集] 匹配里面的任意一个字符
[^字符集] 匹配除了里面的字符意外的所有字符
S1|S2|S3 匹配字符串S1、S2、S3中的任意一个字符串
* 代表多个符号之前的符号,包含0个和1个
+ 代表多个符号之前的符号,至少1个
字符串{N} 字符串出现N次
字符串{M,N} 字符串出现最多N次,最少M次

执行以下SQL以搭建环境

-- 建表
create table info(
    id int primary key,
    name varchar(10)
    );

-- 插入数据
insert into info values
	(1, 'Aric'),
	(2, 'Eric'),
	(3, 'Brt'),
	(4, 'Jack'),
	(5, 'Lucy'),
	(6, 'Lily'),
	(7, 'Tom'),
	(8, 'aaa'),
	(9, 'dadaaa'),
	(10, ''),
	(11, 'abc12'),
	(12, 'ad321'),
	(13, 'ababab');
  1. 查询特定字符或字符串开头的记录

    select * from info where name regexp '^L';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  5 | Lucy |
    |  6 | Lily |
    +----+------+
    */
    
  2. 查询特定字符或字符串结尾的记录

    select * from info where name regexp 'c$';
    /*
    
    */
    
  3. 以符号"."来代替字符串中的任意一个字符

    select * from info where name regexp 'aaa$';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  1 | Aric |
    |  2 | Eric |
    +----+------+
    */
    
  4. 匹配字符串中的任意一个

    select * from info where name regexp '^L..y$';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  5 | Lucy |
    |  6 | Lily |
    +----+------+
    */
    
    select * from info where name regexp '[ceo]';
    /*
    +----+-------+
    | id | name  |
    +----+-------+
    |  1 | Aric  |
    |  2 | Eric  |
    |  4 | Jack  |
    |  5 | Lucy  |
    |  7 | Tom   |
    | 11 | abc12 |
    +----+-------+
    */
    
    select * from info where name regexp '[a-zA-Z]';
    /*
    +----+--------+
    | id | name   |
    +----+--------+
    |  1 | Aric   |
    |  2 | Eric   |
    |  3 | Brt    |
    |  4 | Jack   |
    |  5 | Lucy   |
    |  6 | Lily   |
    |  7 | Tom    |
    |  8 | aaa    |
    |  9 | dadaaa |
    | 11 | abc12  |
    | 12 | ad321  |
    | 13 | ababab |
    +----+--------+
    */
    

    0-9表示数字,a-z表示小写

  5. 匹配指定字符以外的字符

    select * from info where name regexp '[^a-w0-9]';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  5 | Lucy |
    |  6 | Lily |
    +----+------+
    */
    
  6. 匹配指定字符串

    select * from info where name regexp 'ac|uc|ic';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  1 | Aric |
    |  2 | Eric |
    |  4 | Jack |
    |  5 | Lucy |
    +----+------+
    */
    

    |中间不能有空格

  7. 使用"*"和"+"来匹配多个字符

    select * from info where name regexp 'a*c';
    /*
    +----+-------+
    | id | name  |
    +----+-------+
    |  1 | Aric  |
    |  2 | Eric  |
    |  4 | Jack  |
    |  5 | Lucy  |
    | 11 | abc12 |
    +----+-------+
    */
    
    select * from info where name regexp 'a+c';
    /*
    +----+------+
    | id | name |
    +----+------+
    |  4 | Jack |
    +----+------+
    */
    
  8. 使用{N}和{M,N}来指定字符串连续出现的次数

    select * from info where name regexp 'a{3}';
    /*
    +----+--------+
    | id | name   |
    +----+--------+
    |  8 | aaa    |
    |  9 | dadaaa |
    +----+--------+
    */
    
    select * from info where name regexp 'ab{1,3}';
    /*
    +----+--------+
    | id | name   |
    +----+--------+
    | 11 | abc12  |
    | 13 | ababab |
    +----+--------+
    */
    

原文地址:https://www.cnblogs.com/chancey/p/12582964.html