$ mysql -uroot -p
> show databases;
> use demo;
> show tables;
-- 创建表
create table persons(
id int auto_increment primary key,
name varchar(32),
age int,
city varchar(32));
-- 删除表
> drop table persons;
-- 插入数据
insert into persons(name, age, city) value('张三', 20, '北京');
insert into persons(name, age, city) value('李四', 21, '上海');
insert into persons(name, age, city) value('赵倩', 24, '北京');
insert into persons(name, age, city) value('孙力', 23, '天津');
insert into persons(name, age, city) value('周五', 22, '深圳');
insert into persons(name, age, city) value('郑旺', 20, '重庆');
insert into persons(name, age, city) value('刘颖', 20, '北京');
insert into persons(name, age, city) value('王大', 20, '天津');
insert into persons(name, age, city) value('张开', 20, '深圳');
insert into persons(name, age, city) value('留取', 20, '重庆');
-- 查询所有
select * from persons;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 1 | 张三 | 20 | 北京 |
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
-- 查询部分字段
mysql> select name from persons;
+--------+
| name |
+--------+
| 张三 |
| 李四 |
| 赵倩 |
| 孙力 |
| 周五 |
| 郑旺 |
+--------+
mysql> select age from persons;
+------+
| age |
+------+
| 20 |
| 21 |
| 24 |
| 23 |
| 22 |
| 20 |
+------+
-- 去重
mysql> select distinct age from persons;
+------+
| age |
+------+
| 20 |
| 21 |
| 24 |
| 23 |
| 22 |
+------+
-- where条件
mysql> select * from persons where age >22;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
+----+--------+------+--------+
mysql> select * from persons where name='赵倩';
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 3 | 赵倩 | 24 | 北京 |
+----+--------+------+--------+
-- 多条件查询
mysql> select * from persons where age=20 and city='北京';
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 1 | 张三 | 20 | 北京 |
+----+--------+------+--------+
mysql> select * from persons where (age=20 or age =24) and city='北京';
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 1 | 张三 | 20 | 北京 |
| 3 | 赵倩 | 24 | 北京 |
+----+--------+------+--------+
-- 排序,默认从小到大
mysql> select * from persons order by age;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 1 | 张三 | 20 | 北京 |
| 6 | 郑旺 | 20 | 重庆 |
| 2 | 李四 | 21 | 上海 |
| 5 | 周五 | 22 | 深圳 |
| 4 | 孙力 | 23 | 天津 |
| 3 | 赵倩 | 24 | 北京 |
+----+--------+------+--------+
-- 多字段排序
mysql> select * from persons order by age, id desc;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 6 | 郑旺 | 20 | 重庆 |
| 1 | 张三 | 20 | 北京 |
| 2 | 李四 | 21 | 上海 |
| 5 | 周五 | 22 | 深圳 |
| 4 | 孙力 | 23 | 天津 |
| 3 | 赵倩 | 24 | 北京 |
+----+--------+------+--------+
-- 更新数据
mysql> update persons set name='张大拿' where name='张三';
mysql> select * from persons;
+----+-----------+------+--------+
| id | name | age | city |
+----+-----------+------+--------+
| 1 | 张大拿 | 20 | 北京 |
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
+----+-----------+------+--------+
-- 删除数据,慎重!!!
mysql> delete from persons where name='张大拿';
mysql> select * from persons;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
-- 设置查询数量
mysql> select * from persons limit 2;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
+----+--------+------+--------+
-- 模糊查询
mysql> select * from persons where city like '上%';
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
+----+--------+------+--------+
mysql> select * from persons where city not like '上%';
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
-- 成员条件查询
mysql> select * from persons where name in ('赵倩', '周五');
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 3 | 赵倩 | 24 | 北京 |
| 5 | 周五 | 22 | 深圳 |
+----+--------+------+--------+
mysql> select * from persons where name not in ('赵倩', '周五');
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 4 | 孙力 | 23 | 天津 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
-- 范围查询
mysql> select * from persons where age between 22 and 24;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
+----+--------+------+--------+
mysql> select * from persons where age not between 22 and 24;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
-- 创建citys表
create table citys(
id int auto_increment primary key,
city varchar(32),
address varchar(64));
insert into citys(city, address) values('北京', '北京市');
insert into citys(city, address) values('上海', '上海市');
insert into citys(city, address) values('深圳', '广东省');
insert into citys(city, address) values('成都', '四川省');
insert into citys(city, address) values('大连', '辽宁省');
insert into citys(city, address) values('天津', '天津市');
insert into citys(city, address) values('广州', '广东省');
mysql> select * from citys;
+----+--------+-----------+
| id | city | address |
+----+--------+-----------+
| 1 | 北京 | 北京市 |
| 2 | 北京 | 北京市 |
| 3 | 上海 | 上海市 |
| 4 | 深圳 | 广东省 |
| 5 | 成都 | 四川省 |
| 6 | 大连 | 辽宁省 |
| 7 | 天津 | 天津市 |
| 8 | 广州 | 广东省 |
+----+--------+-----------+
-- 修改表名
mysql> alter table cards rename to citys;
-- 别名
mysql> select p.name,p.city,c.address from persons as p, citys as c where p.city=c.city;
+--------+--------+-----------+
| name | city | address |
+--------+--------+-----------+
| 赵倩 | 北京 | 北京市 |
| 赵倩 | 北京 | 北京市 |
| 李四 | 上海 | 上海市 |
| 周五 | 深圳 | 广东省 |
| 孙力 | 天津 | 天津市 |
+--------+--------+-----------+
-- 内连接
mysql> select persons.name, persons.city, citys.address from persons inner join citys on persons.city=citys.city;
+--------+--------+-----------+
| name | city | address |
+--------+--------+-----------+
| 赵倩 | 北京 | 北京市 |
| 赵倩 | 北京 | 北京市 |
| 李四 | 上海 | 上海市 |
| 周五 | 深圳 | 广东省 |
| 孙力 | 天津 | 天津市 |
+--------+--------+-----------+
-- 左连接
mysql> select persons.name, persons.city, citys.address from persons left join citys on persons.city=citys.city;
+--------+--------+-----------+
| name | city | address |
+--------+--------+-----------+
| 赵倩 | 北京 | 北京市 |
| 赵倩 | 北京 | 北京市 |
| 李四 | 上海 | 上海市 |
| 周五 | 深圳 | 广东省 |
| 孙力 | 天津 | 天津市 |
| 郑旺 | 重庆 | NULL |
+--------+--------+-----------+
-- 右连接
mysql> select persons.name, persons.city, citys.address from persons right join citys on persons.city=citys.city;
+--------+--------+-----------+
| name | city | address |
+--------+--------+-----------+
| 李四 | 上海 | 上海市 |
| 赵倩 | 北京 | 北京市 |
| 赵倩 | 北京 | 北京市 |
| 孙力 | 天津 | 天津市 |
| 周五 | 深圳 | 广东省 |
| NULL | NULL | 四川省 |
| NULL | NULL | 辽宁省 |
| NULL | NULL | 广东省 |
+--------+--------+-----------+
# mysql 不支持full join
create table names(
id int auto_increment primary key,
name varchar(32));
insert into names(name) values('李四');
insert into names(name) values('张林');
insert into names(name) values('斗舞');
insert into names(name) values('名天');
insert into names(name) values('高兴');
insert into names(name) values('王二');
mysql> select * from names;
+----+--------+
| id | name |
+----+--------+
| 1 | 李四 |
| 2 | 张林 |
| 3 | 斗舞 |
| 4 | 名天 |
| 5 | 高兴 |
| 6 | 王二 |
+----+--------+
-- 合并结果集
mysql> select name from persons union select name from names;
+--------+
| name |
+--------+
| 李四 |
| 赵倩 |
| 孙力 |
| 周五 |
| 郑旺 |
| 张林 |
| 斗舞 |
| 名天 |
| 高兴 |
| 王二 |
+--------+
-- 列出所有值
mysql> select name from persons union all select name from names;
+--------+
| name |
+--------+
| 李四 |
| 赵倩 |
| 孙力 |
| 周五 |
| 郑旺 |
| 李四 |
| 张林 |
| 斗舞 |
| 名天 |
| 高兴 |
| 王二 |
+--------+
# mysql 不支持select inot
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 添加约束unique
mysql> alter table persons add unique(name);
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | UNI | NULL | |
| age | int(11) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 删除约束unique
mysql> alter table persons drop index name;
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 删除primary key
mysql> alter table persons drop primary key;
mysql> show create table persons \G
Create Table: CREATE TABLE `persons` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`city` varchar(32) DEFAULT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
-- 添加primary key
mysql> alter table persons add primary key(id);
mysql> show create table persons \G
Create Table: CREATE TABLE `persons` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`city` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
-- 外键
create table familys(
id int primary key auto_increment,
person_id int,
menber_num int,
foreign key (person_id) references persons(id));
mysql> show create table familys \G
Create Table: CREATE TABLE `familys` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`person_id` int(11) DEFAULT NULL,
`menber_num` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `person_id` (`person_id`),
CONSTRAINT `familys_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `persons` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
-- 删除外键
mysql> alter table familys drop foreign key familys_ibfk_1;
-- 添加外键
mysql> alter table familys add foreign key(person_id) references persons(id);
-- check约束 InnoDB中不支持check约束
create table student(
id int,
age int,
check(age>0 and age<100));
mysql> show create table student\G
Create Table: CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
-- 默认值default
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 添加默认值
mysql> alter table persons alter age set default 0;
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | 0 | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 删除默认值
mysql> alter table persons alter age drop default;
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 创建索引
mysql> create index person_index on persons(name);
mysql> show create table persons\G
Create Table: CREATE TABLE `persons` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`age` int(11),
`city` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `person_index` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
-- 删除索引
mysql> alter table persons drop index person_index;
mysql> show create table persons\G
Create Table: CREATE TABLE `persons` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`age` int(11),
`city` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
-- 创建数据库
mysql> create database data1;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data1 |
| demo |
| mydata |
| mysql |
| performance_schema |
| sys |
+--------------------+
-- 删除数据库
mysql> drop database data1;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| demo |
| mydata |
| mysql |
| performance_schema |
| sys |
+--------------------+
-- 删除表格
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| citys |
| familys |
| mytable |
| names |
| persons |
| student |
+----------------+
mysql> drop table student;
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| citys |
| familys |
| mytable |
| names |
| persons |
+----------------+
-- 清空数据,保留表结构
mysql> select * from names;
+----+--------+
| id | name |
+----+--------+
| 1 | 李四 |
| 2 | 张林 |
| 3 | 斗舞 |
| 4 | 名天 |
| 5 | 高兴 |
| 6 | 王二 |
+----+--------+
mysql> truncate table names;
mysql> select * from names;
Empty set (0.00 sec)
mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 添加列
mysql> alter table names add alias varchar(32);
mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| alias | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 删除列
mysql> alter table names drop column alias;
mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 修改列属性
mysql> alter table names modify column name varchar(64);
mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 自增删除AUTO_INCREMEN
mysql> alter table names modify column id int;
mysql> desc names;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(64) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
-- 添加自增属性
mysql> alter table names modify id int auto_increment;
mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
-- 添加时间字段
mysql> alter table names add column birthday date;
mysql> desc names;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
mysql> insert into names(name, birthday) values('tom', '2018-2-12');
mysql> select * from names;
+----+------+------------+
| id | name | birthday |
+----+------+------------+
| 1 | tom | 2018-02-12 |
+----+------+------------+
-- null查询
mysql> select * from names;
+----+------+------------+
| id | name | birthday |
+----+------+------------+
| 1 | tom | 2018-02-12 |
| 2 | Jack | NULL |
| 3 | Jimi | NULL |
+----+------+------------+
mysql> select * from names where birthday is null;
+----+------+----------+
| id | name | birthday |
+----+------+----------+
| 2 | Jack | NULL |
| 3 | Jimi | NULL |
+----+------+----------+
-- 函数
-- 平均值
mysql> select * from persons;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
+----+--------+------+--------+
5 rows in set (0.00 sec)
mysql> select avg(age) from persons;
+----------+
| avg(age) |
+----------+
| 22.0000 |
+----------+
-- 计数
mysql> select count(age) from persons;
+------------+
| count(age) |
+------------+
| 5 |
+------------+
-- 取第一个
mysql> select * from persons limit 1;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
+----+--------+------+--------+
-- max min sum
mysql> select min(age) from persons;
+----------+
| min(age) |
+----------+
| 20 |
+----------+
1 row in set (0.00 sec)
mysql> select max(age) from persons;
+----------+
| max(age) |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)
mysql> select sum(age) from persons;
+----------+
| sum(age) |
+----------+
| 110 |
+----------+
-- 分组
mysql> select * from persons;
+----+--------+------+--------+
| id | name | age | city |
+----+--------+------+--------+
| 2 | 李四 | 21 | 上海 |
| 3 | 赵倩 | 24 | 北京 |
| 4 | 孙力 | 23 | 天津 |
| 5 | 周五 | 22 | 深圳 |
| 6 | 郑旺 | 20 | 重庆 |
| 7 | 刘颖 | 20 | 北京 |
| 8 | 王大 | 20 | 天津 |
| 9 | 张开 | 20 | 深圳 |
| 10 | 留取 | 20 | 重庆 |
+----+--------+------+--------+
mysql> select city,sum(age) from persons group by city;
+--------+----------+
| city | sum(age) |
+--------+----------+
| 上海 | 21 |
| 北京 | 44 |
| 天津 | 43 |
| 深圳 | 42 |
| 重庆 | 40 |
+--------+----------+
mysql> select city, sum(age) from persons group by city having sum(age)>40;
+--------+----------+
| city | sum(age) |
+--------+----------+
| 北京 | 44 |
| 天津 | 43 |
| 深圳 | 42 |
+--------+----------+
-- ucase转换为大写
mysql> select * from names;
+----+------+------------+
| id | name | birthday |
+----+------+------------+
| 1 | tom | 2018-02-12 |
| 2 | Jack | NULL |
| 3 | Jimi | NULL |
+----+------+------------+
3 rows in set (0.00 sec)
mysql> select ucase(name), birthday from names;
+-------------+------------+
| ucase(name) | birthday |
+-------------+------------+
| TOM | 2018-02-12 |
| JACK | NULL |
| JIMI | NULL |
+-------------+------------+
-- lcase 转小写
mysql> select lcase(name), birthday from names;
+-------------+------------+
| lcase(name) | birthday |
+-------------+------------+
| tom | 2018-02-12 |
| jack | NULL |
| jimi | NULL |
+-------------+------------+
-- mid截取字符
mysql> select mid(name, 1, 2), birthday from names;
+-----------------+------------+
| mid(name, 1, 2) | birthday |
+-----------------+------------+
| to | 2018-02-12 |
| Ja | NULL |
| Ji | NULL |
+-----------------+------------+
-- round保留小数位
mysql> select avg(age) from persons;
+----------+
| avg(age) |
+----------+
| 21.1111 |
+----------+
1 row in set (0.00 sec)
mysql> select round(avg(age),1) from persons;
+-------------------+
| round(avg(age),1) |
+-------------------+
| 21.1 |
+-------------------+
-- now() 返回当前时间
mysql> select name,now() from names;
+------+---------------------+
| name | now() |
+------+---------------------+
| tom | 2018-05-03 10:49:09 |
| Jack | 2018-05-03 10:49:09 |
| Jimi | 2018-05-03 10:49:09 |
+------+---------------------+