数据库的完整使用1

1.数据库的基本操作

1.1查询所有数据库

show databases ;

1.2 查询当前数据库

select database();

1.3 创建数据库

create database [ if not exists ] 数据库名 [ default charset 字符集 ] [ collate 排序
规则 ] ;

创建一个itcast数据库, 使用数据库默认的字符集。

create database itcast;

在同一个数据库服务器中,不能创建两个名称相同的数据库,否则将会报错。 可以通过if not exists 参数来解决这个问题,数据库不存在, 则创建该数据库,如果存在,则不创建。

create database if not extists itcast;

创建一个itheima数据库,并且指定字符集

create database itheima default charset utf8mb4;

1.4 删除数据库

drop database [ if exists ] 数据库名 ;

如果删除一个不存在的数据库,将会报错。此时,可以加上参数 if exists ,如果数据库存在,再 执行删除,否则不执行删除。

1.5 切换数据库

use 数据库名 ;

我们要操作某一个数据库下的表时,就需要通过该指令,切换到对应的数据库下,否则是不能操作的。 比如,切换到itcast数据,执行如下SQL:

use itcast;

2.数据库表的基本操作

2.1 操作-查询创建

2.1.1 查询当前数据库所有表

show tables;

比如,我们可以切换到sys这个系统数据库,并查看系统数据库中的所有表结构。

use sys;
show tables;

image.png2.1.2 查看指定表结构

desc 表名 ;

通过这条指令,我们可以查看到指定表的字段,字段的类型、是否可以为NULL,是否存在默认值等信 息。 image.png

2.1.3 查询指定表的建表语句

show create table 表名 ;

通过这条指令,主要是用来查看建表语句的,而有部分参数我们在创建表的时候,并未指定也会查询 到,因为这部分是数据库的默认值,如:存储引擎、字符集等。 image.png

2.1.4 创建表结构

CREATE TABLE 表名(
字段1 字段1类型 [ COMMENT 字段1注释 ],
字段2 字段2类型 [COMMENT 字段2注释 ],
字段3 字段3类型 [COMMENT 字段3注释 ],
......
字段n 字段n类型 [COMMENT 字段n注释 ]
) [ COMMENT 表注释 ] ;

注意: [...] 内为可选参数,最后一个字段后面没有逗号

比如,我们创建一张表 tb_user ,对应的结构如下,那么建表语句为: image.png

mysql>
create table tb_user(
	id int , 
	name varchar(50),
	age int, 
	gender varchar(1) );

或者
create table tb_user(
	id int comment '编号',
	name varchar(50) comment '姓名',
	age int comment '年龄',
	gender varchar(1) comment '性别'
) comment '用户表';

image.png

2.2 表操作-数据类型

在上述的建表语句中,我们在指定字段的数据类型时,用到了int ,varchar,那么在MySQL中除了 以上的数据类型,还有哪些常见的数据类型呢? 接下来,我们就来详细介绍一下MySQL的数据类型。 MySQL中的数据类型有很多,主要分为三类:数值类型、字符串类型、日期时间类型。

2.2.1 数值类型

image.png

1). 年龄字段 -- 不会出现负数, 而且人的年龄不会太大
age tinyint unsigned
2). 分数 -- 总分100分, 最多出现一位小数
score double(4,1)

2.2.2 字符串类型

image.png
char 与 varchar 都可以描述字符串,char是定长字符串,指定长度多长,就占用多少个字符,和字段值的长度无关 。而varchar是变长字符串,指定的长度为最大占用长度 。相对来说,char的性能会更高些。

1). 用户名 username ------> 长度不定, 最长不会超过50
username varchar(50)
2). 性别 gender ---------> 存储值, 不是男,就是女
gender char(1)
3). 手机号 phone --------> 固定长度为11
phone char(11)

2.2.3 日期时间类型

image.png

1). 生日字段 birthday
birthday date
2). 创建时间 createtime
createtime datetime

2.2.4 表操作-案例

image.png

create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
entrydate date comment '入职时间'
) comment '员工表';
mysql> create table emp( id int comment '编号', workno varchar(10) comment '员工工号', name varchar(10) comment '员工姓名', genter char(1) comment '性别', age tinyint unsigned comment '年龄', idcard char(18) comment '身份证号', entrydate date comment '入职时间' )comment '员工表';
Query OK, 0 rows affected (0.02 sec)

mysql> 

SQL语句编写完毕之后,就可以在MySQL的命令行中执行SQL,然后也可以通过 desc 指令查询表结构 信息: image.png
表结构创建好了,里面的name字段是varchar类型,最大长度为10,也就意味着如果超过10将会报 错,如果我们想修改这个字段的类型 或 修改字段的长度该如何操作呢?接下来再来讲解DDL语句中, 如何操作表字段。

2.3 表操作-修改

2.3.1 添加字段

ALTER TABLE 表名 ADD 字段名 类型 (长度) [ COMMENT 注释 ] [ 约束 ];

为emp表增加一个新的字段”昵称”为nickname,类型为varchar(20)

mysql> alter table emp add nickname varchar(20) comment'昵称';
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

image.png

2.3.2 修改数据类型

ALTER TABLE 表名 MODIFY 字段名 新数据类型 (长度);

2.3.3 修改字段名和字段类型

ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型 (长度) [ COMMENT 注释 ] [ 约束 ];

案例: 将emp表的nickname字段修改为username,类型为varchar(30)

mysql> alter table emp change nickname username varchar(30) comment '昵称';

image.png

2.3.4 删除字段

ALTER TABLE 表名 DROP 字段名;

案例: 将emp表的字段username删除

alter table emp drop username;

image.png

2.4 表操作-删除

2.4.1 删除表

DROP TABLE [ IF EXISTS ] 表名;

可选项 IF EXISTS 代表,只有表名存在时才会删除该表,表名不存在,则不执行删除操作(如果不 加该参数项,删除一张不存在的表,执行将会报错)。
案例: 如果tb_user表存在,则删除tb_user表

DROP TABLE IF EXISTS tb_user;

mysql> DROP TABLE IF EXISTS tb_user;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-----------------+
| Tables_in_itzfl |
+-----------------+
| emp             |
+-----------------+
1 row in set (0.00 sec)

2.4.2 删除指定表, 并重新创建表

TRUNCATE TABLE 表名;

注意: 在删除表的时候,表中的全部数据也都会被删除;s

2.5 DML

DML英文全称是Data Manipulation Language(数据操作语言),用来对数据库中表的数据记录进 行增、删、改操作。 添加数据(INSERT) 修改数据(UPDATE) 删除数据(DELETE)

2.5.1 添加数据

2.5.1.1 给指定字段添加数据
INSERT INTO 表名 (字段名1, 字段名2, ...) VALUES (值1, 值2, ...);

给emp表所有的字段添加数据

insert into emp(id,workno,name,gender,age,idcard,entrydate)
values(1,'1','Itcast','男',10,'123456789012345678','2000-01-01');
mysql> select * from emp;
+------+--------+--------+--------+------+--------------------+------------+
| id   | workno | name   | gender | age  | idcard             | entrydate  |
+------+--------+--------+--------+------+--------------------+------------+
|    1 | 1      | Itcast | 男     |   10 | 123456789012345678 | 2000-01-01 |
+------+--------+--------+--------+------+--------------------+------------+
1 row in set (0.00 sec)
2.5.1.2 给全部字段添加数据
INSERT INTO 表名 VALUES (值1, 值2, ...);

案例:插入数据到emp表,具体的SQL如下:

insert into emp values(2,'2','张无忌','男',18,'123456789012345670','2005-01-
01');
mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    1 | 1      | Itcast    | 男     |   10 | 123456789012345678 | 2000-01-01 |
|    2 | 2      | 张无忌    | 男     |   18 | 123456789012345670 | 2005-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+
2 rows in set (0.00 sec)
2.5.1.3 批量添加数据
INSERT INTO 表名 (字段名1, 字段名2, ...) VALUES (值1, 值2, ...), (值1, 值2, ...), (值
1, 值2, ...) ;
 INSERT INTO 表名 VALUES (值1, 值2, ...), (值1, 值2, ...), (值1, 值2, ...) ;

案例:批量插入数据到employee表,具体的SQL如下:

insert into emp values(3,'3','韦一笑','男',38,'123456789012345670','2005-01-01'),(4,'4','赵敏','女',18,'123456789012345670','2005-01-01');
mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    1 | 1      | Itcast    | 男     |   10 | 123456789012345678 | 2000-01-01 |
|    2 | 2      | 张无忌    | 男     |   18 | 123456789012345670 | 2005-01-01 |
|    3 | 3      | 韦一笑    | 男     |   38 | 123456789012345670 | 2005-01-01 |
|    4 | 4      | 赵敏      | 女     |   18 | 123456789012345670 | 2005-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+

注意事项: • 插入数据时,指定的字段顺序需要与值的顺序是一一对应的。
• 字符串和日期型数据应该包含在引号中。
• 插入的数据大小,应该在字段的规定范围内。

2.5.2 修改数据

UPDATE 表名 SET 字段名1 = 值1 , 字段名2 = 值2 , .... [ WHERE 条件 ] ;

案例:
A. 修改id为1的数据,将name修改为itheima

update emp set name = 'itheima' where id = 1;
mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    1 | 1      | ithrima   | 男     |   10 | 123456789012345678 | 2000-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+

B. 修改id为1的数据, 将name修改为小昭, gender修改为 女

mysql> update emp set name = '小昭' , gender = '女' where id=1;

mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    1 | 1      | 小昭      | 女     |   10 | 123456789012345678 | 2000-01-01 |
|    2 | 2      | 张无忌    | 男     |   18 | 123456789012345670 | 2005-01-01 |
|    3 | 3      | 韦一笑    | 男     |   38 | 123456789012345670 | 2005-01-01 |
|    4 | 4      | 赵敏      | 女     |   18 | 123456789012345670 | 2005-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+

C. 将所有的员工入职日期修改为 2008-01-01

mysql> update emp set entrydate = '2008-01-01';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    1 | 1      | 小昭      | 女     |   10 | 123456789012345678 | 2008-01-01 |
|    2 | 2      | 张无忌    | 男     |   18 | 123456789012345670 | 2008-01-01 |
|    3 | 3      | 韦一笑    | 男     |   38 | 123456789012345670 | 2008-01-01 |
|    4 | 4      | 赵敏      | 女     |   18 | 123456789012345670 | 2008-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+

注意事项:
修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据。

2.5.3 删除数据

DELETE FROM 表名 [ WHERE 条件 ] ;

A. 删除gender为女的员工

mysql> delete from emp where gender = '女';
Query OK, 2 rows affected (0.01 sec)

mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+------------+
| id   | workno | name      | gender | age  | idcard             | entrydate  |
+------+--------+-----------+--------+------+--------------------+------------+
|    2 | 2      | 张无忌    | 男     |   18 | 123456789012345670 | 2008-01-01 |
|    3 | 3      | 韦一笑    | 男     |   38 | 123456789012345670 | 2008-01-01 |
+------+--------+-----------+--------+------+--------------------+------------+
2 rows in set (0.00 sec)

B. 删除所有员工

delete from emp;
mysql> delete from emp;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from emp;
Empty set (0.00 sec)
注意事项:
• DELETE 语句的条件可以有,也可以没有,如果没有条件,则会删除整张表的所有数
据。
• DELETE 语句不能删除某一个字段的值(可以使用UPDATE,将该字段值置为NULL即
可)。
• 当进行删除全部数据操作时,datagrip会提示我们,询问是否确认删除,我们直接点击
Execute即可。

2.6 DQL

DQL英文全称是Data Query Language(数据查询语言),数据查询语言,用来查询数据库中表的记 录。
查询关键字: SELECT 在一个正常的业务系统中,查询操作的频次是要远高于增删改的,当我们去访问企业官网、电商网站, 在这些网站中我们所看到的数据,实际都是需要从数据库中查询并展示的。而且在查询的过程中,可能 还会涉及到条件、排序、分页等操作。

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');

mysql> select * from emp;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
16 rows in set (0.00 sec)

2.6.1 基本语法

SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后条件列表
ORDER BY
排序字段列表
LIMIT
分页参数

我们在讲解这部分内容的时候,会将上面的完整语法进行拆分,分为以下几个部分:
基本查询(不带任何条件)
条件查询(WHERE)
聚合函数(count、max、min、avg、sum)
分组查询(group by)
排序查询(order by)
分页查询(limit)

2.6.2 基础查询

2.6.2.1 查询多个字段
SELECT 字段1, 字段2, 字段3 ... FROM 表名 ;
SELECT * FROM 表名 ;

注意 : * 号代表查询所有字段,在实际开发中尽量少用(不直观、影响效率)。
A. 查询指定字段 name, workno, age并返回

mysql> select name,workno,age from emp;
+-----------+--------+------+
| name      | workno | age  |
+-----------+--------+------+
| 柳岩666   | 00001  |   20 |
| 张无忌    | 00002  |   18 |
| 韦一笑    | 00003  |   38 |
| 赵敏      | 00004  |   18 |
| 小昭      | 00005  |   16 |
| 杨逍      | 00006  |   28 |
| 范瑶      | 00007  |   40 |
| 黛绮丝    | 00008  |   38 |
| 范凉凉    | 00009  |   45 |
| 陈友谅    | 00010  |   53 |
| 张士诚    | 00011  |   55 |
| 常遇春    | 00012  |   32 |
| 张三丰    | 00013  |   88 |
| 灭绝      | 00014  |   65 |
| 胡青牛    | 00015  |   70 |
| 周芷若    | 00016  |   18 |
+-----------+--------+------+
16 rows in set (0.00 sec)

B. 查询返回所有字段

 select id ,workno,name,gender,age,idcard,workaddress,entrydate from emp;

 select * from emp;
2.6.2.2 字段设置别名
SELECT 字段1 [ AS 别名1 ] , 字段2 [ AS 别名2 ] ... FROM 表名;
 SELECT 字段1 [ 别名1 ] , 字段2 [ 别名2 ] ... FROM 表名

查询所有员工的工作地址,起别名

mysql> select workaddress as '工作地址' from emp;
+--------------+
| 工作地址     |
+--------------+
| 北京         |
| 北京         |
| 上海         |
| 北京         |
| 上海         |
| 北京         |
| 北京         |
| 天津         |
| 北京         |
| 上海         |
| 江苏         |
| 北京         |
| 江苏         |
| 西安         |
| 西安         |
| 北京         |
+--------------+
16 rows in set (0.00 sec)


-- as可以省略
select workaddress '工作地址' from emp;

2.6.2.3 去除重复记录
SELECT DISTINCT 字段列表 FROM 表名;

查询公司员工的上班地址有哪些(不要重复)

mysql> select distinct workaddress '工作地址' from emp;
+--------------+
| 工作地址     |
+--------------+
| 北京         |
| 上海         |
| 天津         |
| 江苏         |
| 西安         |
+--------------+
5 rows in set (0.01 sec)

mysql> 

2.6.3条件查询

2.6.3.1 语法
SELECT 字段列表 FROM 表名 WHERE 条件列表 ;
2.6.3.2 条件

常用的比较运算符如下: image.png
常用的逻辑运算符如下:
image.png
案列:
A. 查询年龄等于 88 的员工

select * from emp where age = 88;
mysql> select * from emp where age = 88;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
1 row in set (0.00 sec)

B. 查询年龄小于 20 的员工信息

 select * from emp where age < 20;

mysql>  select * from emp where age < 20;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
4 rows in set (0.00 sec)

D. 查询没有身份证号的员工信息

select * from emp where idcard is null;
mysql> select * from emp where idcard is null;
+------+--------+-----------+--------+------+--------+-------------+------------+
| id   | workno | name      | gender | age  | idcard | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------+-------------+------------+
|   16 | 00016  | 周芷若    | 女     |   18 | NULL   | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------+-------------+------------+
1 row in set (0.00 sec)

mysql> 

E.查询有身份证号的员工信息

select * from emp where idcard is not null;
mysql> select * from emp where idcard is not null;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
15 rows in set (0.00 sec)

F. 查询年龄不等于 88 的员工信息

select * from emp where age != 88;
select * from emp where age <> 88;
mysql> select * from emp where age != 88;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
15 rows in set (0.00 sec)

G. 查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息

select * from emp where age >= 15 && age <= 20;
select * from emp where age >= 15 and age <= 20;
select * from emp where age between 15 and 20;
mysql> select * from emp where age >= 15 && age <= 20;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
5 rows in set, 1 warning (0.00 sec)

H. 查询性别为 女 且年龄小于 25岁的员工信息

select * from emp where gender = '女' and age < 25;
mysql> select * from emp where gender = '女' and age < 25; 
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
4 rows in set (0.00 sec)

I. 查询年龄等于18 或 20 或 40 的员工信息

select * from emp where age = 18 or age = 20 or age =40;
select * from emp where age in(18,20,40);

J. 查询姓名为两个字的员工信息 _ %

mysql> select * from emp where name like '__';
+------+--------+--------+--------+------+--------------------+-------------+------------+
| id   | workno | name   | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+--------+--------+------+--------------------+-------------+------------+
|    4 | 00004  | 赵敏   | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭   | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    6 | 00006  | 杨逍   | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|    7 | 00007  | 范瑶   | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|   14 | 00014  | 灭绝   | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
+------+--------+--------+--------+------+--------------------+-------------+------------+
5 rows in set (0.00 sec)

mysql> 

K. 查询身份证号最后一位是X的员工信息

select * from emp where idcard like '%X';
select * from emp where idcard like '_________________X';
mysql> select * from emp where idcard like '%X';
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
2 rows in set (0.00 sec)

2.6.4聚合函数

2.6.4.1 介绍

将一列数据作为一个整体,进行纵向计算 。

2.6.4.2 常见的聚合函数

image.png

2.6.4.3 语法
SELECT 聚合函数(字段列表) FROM 表名 ;

注意 : NULL值是不参与所有聚合函数运算的。

案例:
A. 统计该企业员工数量

select count(*) from emp; -- 统计的是总记录数
select count(idcard) from emp; -- 统计的是idcard字段不为null的记录数
mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
|       16 |
+----------+
1 row in set (0.01 sec)

mysql> select count(idcard) from emp;
+---------------+
| count(idcard) |
+---------------+
|            15 |
+---------------+
1 row in set (0.00 sec)

mysql> 

对于count聚合函数,统计符合条件的总记录数,还可以通过 count(数字/字符串)的形式进行统计 查询,比如:

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

mysql> 

B. 统计该企业员工的平均年龄

select avg(age) from emp;
mysql> select avg(age) from emp;
+----------+
| avg(age) |
+----------+
|  40.1250 |
+----------+
1 row in set (0.01 sec)


C. 统计该企业员工的最大年龄

select max(age) from emp;
mysql> select max(age) from emp;
+----------+
| max(age) |
+----------+
|       88 |
+----------+
1 row in set (0.00 sec)

mysql> 

D. 统计该企业员工的最小年龄

select min(age) from emp;
mysql> select min(age) from emp;
+----------+
| min(age) |
+----------+
|       16 |
+----------+
1 row in set (0.00 sec)

E. 统计西安地区员工的年龄之和

select sum(age) from emp where workaddress = '西安';
mysql> select sum(age) from emp where workaddress = '西安';
+----------+
| sum(age) |
+----------+
|      135 |
+----------+
1 row in set (0.00 sec)

mysql> 

2.6.5 分组查询

2.6.5.1 语法
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组
后过滤条件 ]
2.6.5.2 where与having区别
  • 执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;
  • 而having是分组 之后对结果进行过滤。 判断条件不同:where不能对聚合函数进行判断,而having可以。
注意事项:
• 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。
• 执行顺序: where > 聚合函数 > having 。
• 支持多字段分组, 具体语法为 : group by columnA,columnB

案例:
A. 根据性别分组 , 统计男性员工 和 女性员工的数量

select gender, count(*) from emp group by gender ;
mysql> select gender, count(*) from emp group by gender ;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 女     |        7 |
| 男     |        9 |
+--------+----------+
2 rows in set (0.00 sec)

B. 根据性别分组 , 统计男性员工 和 女性员工的平均年龄

select gender, avg(age) from emp group by gender ;
mysql> select gender, avg(age) from emp group by gender ;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 女     |  31.4286 |
| 男     |  46.8889 |
+--------+----------+
2 rows in set (0.01 sec)

C. 查询年龄小于45的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址

select workaddress, count(*) address_count from emp where age < 45 group by workaddress having address_count >= 3;

mysql> select workaddress, count(*) address_count from emp where age < 45 group by workaddress having address_count >= 3;
+-------------+---------------+
| workaddress | address_count |
+-------------+---------------+
| 北京        |             7 |
+-------------+---------------+
1 row in set (0.00 sec)

D. 统计各个工作地址上班的男性及女性员工的数量

select workaddress, gender, count(*) '数量' from emp group by gender , workaddress;
mysql> select workaddress, gender, count(*) '数量' from emp group by gender , workaddress;
+-------------+--------+--------+
| workaddress | gender | 数量   |
+-------------+--------+--------+
| 北京        | 女     |      4 |
| 北京        | 男     |      4 |
| 上海        | 男     |      2 |
| 上海        | 女     |      1 |
| 天津        | 女     |      1 |
| 江苏        | 男     |      2 |
| 西安        | 女     |      1 |
| 西安        | 男     |      1 |
+-------------+--------+--------+
8 rows in set (0.01 sec)

2.6.6 排序查询

排序在日常开发中是非常常见的一个操作,有升序排序,也有降序排序。

2.6.6.1 语法
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1 , 字段2 排序方式2 ;
2.6.6.2 排序方式

ASC : 升序(默认值)
DESC: 降序
注意事项:
• 如果是升序, 可以不指定排序方式ASC ;
• 如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序 ;
案例:
A. 根据年龄对公司的员工进行升序排序

select * from emp order by age asc;
select * from emp order by age;
mysql> select * from emp order by age asc;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
16 rows in set (0.00 sec)

mysql> 

B. 根据入职时间, 对员工进行降序排序

select * from emp order by entrydate desc;
mysql> select * from emp order by entrydate desc;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
16 rows in set (0.00 sec)

C. 根据年龄对公司的员工进行升序排序 , 年龄相同 , 再按照入职时间进行降序排序

select * from emp order by age asc , entrydate desc;
mysql> select * from emp order by age asc , entrydate desc;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    5 | 00005  | 小昭      | 女     |   16 | 123456769012345678 | 上海        | 2007-07-01 |
|   16 | 00016  | 周芷若    | 女     |   18 | NULL               | 北京        | 2012-06-01 |
|    4 | 00004  | 赵敏      | 女     |   18 | 123456757123845670 | 北京        | 2009-12-01 |
|    2 | 00002  | 张无忌    | 男     |   18 | 123456789012345670 | 北京        | 2005-09-01 |
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|    8 | 00008  | 黛绮丝    | 女     |   38 | 123456157123645670 | 天津        | 2015-05-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
|    9 | 00009  | 范凉凉    | 女     |   45 | 123156789012345678 | 北京        | 2010-04-01 |
|   10 | 00010  | 陈友谅    | 男     |   53 | 123456789012345670 | 上海        | 2011-01-01 |
|   11 | 00011  | 张士诚    | 男     |   55 | 123567897123465670 | 江苏        | 2015-05-01 |
|   14 | 00014  | 灭绝      | 女     |   65 | 123456719012345670 | 西安        | 2019-05-01 |
|   15 | 00015  | 胡青牛    | 男     |   70 | 12345674971234567X | 西安        | 2018-04-01 |
|   13 | 00013  | 张三丰    | 男     |   88 | 123656789012345678 | 江苏        | 2020-11-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+

2.6.7 分页查询

分页操作在业务系统开发时,也是非常常见的一个功能,我们在网站中看到的各种各样的分页条,后台 都需要借助于数据库的分页操作。
1). 语法

SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数 ;
注意事项:
• 起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数。
• 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。
• 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10。

案例:
A. 查询第1页员工数据, 每页展示10条记录

select * from emp limit 0,10;
select * from emp limit 10;

B. 查询第2页员工数据, 每页展示10条记录 --------> (页码-1)*页展示记录数

select * from emp limit 10,10;

3.案例练习

1). 查询年龄为20,21,22,23岁的员工信息。

mysql> select * from emp where age in(20,21,22,23);
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    1 | 00001  | 柳岩666   | 女     |   20 | 123456789012345678 | 北京        | 2000-01-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
1 row in set (0.00 sec)

mysql> 

2). 查询性别为 男 ,并且年龄在 20-40 岁(含)以内的姓名为三个字的员工。

mysql> select * from emp where gender = '男' and ( age between 20 and 40 ) and name like '___';
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
2 rows in set (0.00 sec)

mysql> select * from emp where gender = '男' and ( age > 20 && age < 40 ) and name like '___';
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
2 rows in set, 1 warning (0.00 sec)

mysql> 

3). 统计员工表中, 年龄小于60岁的 , 男性员工和女性员工的人数。

select gender, count(*) from emp where age < 60 group by gender;

mysql> select gender, count(*) from emp where age < 60 group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 女     |        6 |
| 男     |        7 |
+--------+----------+
2 rows in set (0.00 sec)

4). 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按 入职时间降序排序。

mysql> select name , age from emp where age <= 35 order by age asc , entrydate desc;
+-----------+------+
| name      | age  |
+-----------+------+
| 小昭      |   16 |
| 周芷若    |   18 |
| 赵敏      |   18 |
| 张无忌    |   18 |
| 柳岩666   |   20 |
| 杨逍      |   28 |
| 常遇春    |   32 |
+-----------+------+
7 rows in set (0.00 sec)

mysql> 

5). 查询性别为男,且年龄在20-40 岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序, 年龄相同按入职时间升序排序。

mysql> select * from emp where gender = '男' and age between 20 and 40 order by age asc ,entrydate asc limit 5 ;
+------+--------+-----------+--------+------+--------------------+-------------+------------+
| id   | workno | name      | gender | age  | idcard             | workaddress | entrydate  |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
|    6 | 00006  | 杨逍      | 男     |   28 | 12345678931234567X | 北京        | 2006-01-01 |
|   12 | 00012  | 常遇春    | 男     |   32 | 123446757152345670 | 北京        | 2004-02-01 |
|    3 | 00003  | 韦一笑    | 男     |   38 | 123456789712345670 | 上海        | 2005-08-01 |
|    7 | 00007  | 范瑶      | 男     |   40 | 123456789212345670 | 北京        | 2005-05-01 |
+------+--------+-----------+--------+------+--------------------+-------------+------------+
4 rows in set (0.00 sec)

DQL语句的执行顺序为: from … where … group by … having … select … order by … limit …

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值