一、插入
select * from mysql.user; # 会显示的乱,由于一行内容(字段记录)太多屏幕显示不开
select * from mysql.user\G #把一条记录按行显示
把原来一张表里面的几个记录或(一张表里有多条记录但是有用的仅仅有几条)迁移到一张新的表里,那么就可以这样:
需要host字段与user字段:
create table user(host char(60),user char(16)) select host,user from mysql.use
注意:如果在新建的表里字段和原表的字段不一样就会出现以下情况会把IP与port当成全新的字段而且是空记录
mysql> create table user1(ip char(60),port char(16)) select host,user from mysql.user;
Query OK, 4 rows affected (0.27 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from user1;
+------+------+-----------+---------------+
| ip | port | host | user |
+------+------+-----------+---------------+
| NULL | NULL | % | wang |
| NULL | NULL | localhost | mysql.session |
| NULL | NULL | localhost | mysql.sys |
| NULL | NULL | localhost | root |
+------+------+-----------+---------------+
4 rows in set (0.00 sec)
mysql> desc user1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| ip | char(60) | YES | | NULL | |
| port | char(16) | YES | | NULL | |
| host | char(60) | NO | | | |
| user | char(32) | NO | | | |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
别名:as关键字,不加也行
这样也是可以的:
create table user2(ip char(60),username char(16)) select host as ip,user as username from mysql.user;
查询时也可以取个别名:
mysql> select host 主机名,user 用户名 from mysql.user;
+-----------+---------------+
| 主机名 | 用户名 |
+-----------+---------------+
| % | wang |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
二、更新与删除
更新:update db1.t1 set 字段=值 where id>10 or id < 30;
update mysql.user set password=password('123')
where host='localhost' and user='root';
删除表中的某一个字段:
delete from 表名 where 字段名 条件 字段内容;
清空表:
truncate 表名;
三、单表查询语法与简单查询:
单表查询语法:
select distinct 字段1,字段2,字段3。。。。 from 表名
where 约束条件
group by 分组的字段
having 过滤条件
order by 排序字段
limit 限制条件;
顺序是:
(1)先找到这张表from,
(2)看看有么有约束条件,拿着where指定的约束条件,去文件/表中取出一条条记录,
(3)将取出的一条条记录进行group by分组,如果没有group by,则整体作为一组,
(4)将分组的结果进行having过滤,
(5)执行select,
(6)distinct去重,
(7)将结果按条件排序:order by,
(8)限制结果的显示条数)。
company.employee
员工id id int
姓名 emp_name varchar
性别 sex enum
年龄 age int
入职日期 hire_date date
岗位 post varchar
职位描述 post_comment varchar
薪水 salary double
办公室 office int
部门编号 depart_id int
#创建表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);
#查看表结构
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
#插入记录
#三个部门:教学,销售,运营
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龙','male',48,'20101111','teacher',10000,401,1),
('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),
('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;
准备表和记录
#简单查询
select * from employee;
select name,salary from employee;
#避免重复distinct
select distinct post from employee;
通过四则运算查询
selct name, salary*12 FROM employee;
select name, salary*12 as 年薪 from employee;
select name 名字, salary*12 年薪 from employee;
select name, salary+1000 as 月薪 from employee;
mysql> select name, salary*12 as 年薪 from employee;
+------------+-------------+
| name | 年薪 |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+-------------+
18 rows in set (0.00 sec)
mysql> select name 名字, salary*12 年薪 from employee;
+------------+-------------+
| 名字 | 年薪 |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+-------------+
18 rows in set (0.01 sec)
mysql> select name, salary+1000 as 月薪 from employee;
+------------+------------+
| name | 月薪 |
+------------+------------+
| egon | 8300.33 |
| alex | 1001000.31 |
| wupeiqi | 9300.00 |
| yuanhao | 4500.00 |
| liwenzhou | 3100.00 |
| jingliyang | 10000.00 |
| jinxin | 31000.00 |
| 成龙 | 11000.00 |
| 歪歪 | 4000.13 |
| 丫丫 | 3000.35 |
| 丁丁 | 2000.37 |
| 星星 | 4000.29 |
| 格格 | 5000.33 |
| 张野 | 11000.13 |
| 程咬金 | 21000.00 |
| 程咬银 | 20000.00 |
| 程咬铜 | 19000.00 |
| 程咬铁 | 18000.00 |
+------------+------------+
18 rows in set (0.00 sec)
定义显示格式
concat() 函数用于连接字符串
select concat('姓名: ',name,' 年薪: ', salary*12) from employee;
mysql> select concat('姓名: ',name,' 年薪: ', salary*12) from employee;
+---------------------------------------------+
| concat('姓名: ',name,' 年薪: ', salary*12) |
+---------------------------------------------+
| 姓名: egon 年薪: 87603.96 |
| 姓名: alex 年薪: 12000003.72 |
| 姓名: wupeiqi 年薪: 99600.00 |
| 姓名: yuanhao 年薪: 42000.00 |
| 姓名: liwenzhou 年薪: 25200.00 |
| 姓名: jingliyang 年薪: 108000.00 |
| 姓名: jinxin 年薪: 360000.00 |
| 姓名: 成龙 年薪: 120000.00 |
| 姓名: 歪歪 年薪: 36001.56 |
| 姓名: 丫丫 年薪: 24004.20 |
| 姓名: 丁丁 年薪: 12004.44 |
| 姓名: 星星 年薪: 36003.48 |
| 姓名: 格格 年薪: 48003.96 |
| 姓名: 张野 年薪: 120001.56 |
| 姓名: 程咬金 年薪: 240000.00 |
| 姓名: 程咬银 年薪: 228000.00 |
| 姓名: 程咬铜 年薪: 216000.00 |
| 姓名: 程咬铁 年薪: 204000.00 |
+---------------------------------------------+
concat_ws() 第一个参数为分隔符
select concat_ws(':',name,salary*12) from employee;
mysql> select concat_ws(':',name,salary*12) from employee;
+-------------------------------+
| concat_ws(':',name,salary*12) |
+-------------------------------+
| egon:87603.96 |
| alex:12000003.72 |
| wupeiqi:99600.00 |
| yuanhao:42000.00 |
| liwenzhou:25200.00 |
| jingliyang:108000.00 |
| jinxin:360000.00 |
| 成龙:120000.00 |
| 歪歪:36001.56 |
| 丫丫:24004.20 |
| 丁丁:12004.44 |
| 星星:36003.48 |
| 格格:48003.96 |
| 张野:120001.56 |
| 程咬金:240000.00 |
| 程咬银:228000.00 |
| 程咬铜:216000.00 |
| 程咬铁:204000.00 |
+-------------------------------+
18 rows in set (0.00 sec)
四、where约束条件
1. 比较运算符:> < >= <= <> !=
2. between 80 and 100 值在80到100之间
3. in(80,90,100) 值是80或90或100
4. like 'egon%'
pattern可以是%或_,
%表示任意多字符
_表示任意一个字符
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
select id,name from employee where post='teacher';
select id,name from employee where id >=3 and id < 5;
select id,name from employee where id <=3 or id >= 5;
select id,name from employee where id between 3 and 5;
select id,name from employee where id >=3 and id <= 5;
select id,name,age from employee
where age = 38 or age = 28 or age = 48 or age = 18;
select id,name,age from employee
where age not in (60,70,80,18,'aaa');
select id,name,age from employee
where name like 'e%'; # 名字以e开头的
select id,name,age from employee
where name not like '___'; #名字有3个字符的
# null并不是空字符串
mysql> select * from employee where not post_comment is null;
Empty set (0.02 sec) mysql> select * from employee where not post_comment is ' ';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' '' at line 1
1. 查看岗位是teacher的员工姓名、年龄
2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄
3. 查看岗位是teacher且薪资在9000-10000范围内的员工姓名、年龄、薪资
4. 查看岗位描述不为NULL的员工信息
5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资
6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资
7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪
练习
1、select name,age from employee where post='teacher';
2、select name,age from employee where post='teacher' and age > 30;
3、select name,age,salary from employee where post='teacher' and salary between 9000 and 10000;
4、select * from employee where not post_comment is null;
5、select name,age,salary from employee where post='teacher' and salary in (10000,9000,30000);
6、select name,age,salary from employee where post='teacher' and salary not in (10000,9000,30000);
7、select name,salary*12 from employee where post='teacher' and name like 'jin%';
查询结果
五、分组:group by
1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的
2、分组指的是:将所有记录按照某个相同的字段进行归类,比如针对员工信息表的职位分组,或者按照性别分组等
3、查询条件里有‘每’这个字,就是我们分组的依据
4、可以按照任意字段分组,但是分组后只能查看分组的字段,如果想查看组内信息需要借助于聚合函数
#强调:
1、分组之后,select只能查看到分组的字段,要想查组内内容
不能直接查看,需要借助于聚合函数max,min,avg,sum,count
2、分组的目的是为类以组为单位来处理记录,而不是处理单独的记录
3、如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
ONLY_FULL_GROUP_BY
在5.6中貌似ONLY_FULL_GROUP_BY是没设置的,那么问题来了:
如果统计每个部门的员工姓名,写成这样就会变成如下图所示,但实际不是这样的
所以,为了不让这种情况出现,我们可以修改下配置文件
set global sql_mode = 'only_full_group_by'; 设置,设置完后退出重新登录
select @@global.sql_mode; 查看
设置完后就会报错
#查看MySQL 5.7默认的sql_mode如下:
mysql> select @@global.sql_mode;
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#!!!注意
ONLY_FULL_GROUP_BY的语义就是确定select target list中的所有列的值都是明确语义,简单的说来,在ONLY_FULL_GROUP_BY模式下,target list中的值要么是来自于聚集函数的结果,要么是来自于group by list中的表达式的值。
#设置sql_mole如下操作(我们可以去掉ONLY_FULL_GROUP_BY模式):
mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
!!!sql_mode设置
六、聚合函数
常用于GROUP BY从句的SELECT查询中
AVG(col)返回指定列的平均值
COUNT(col)返回指定列中非NULL值的个数
MIN(col)返回指定列的最小值
MAX(col)返回指定列的最大值
SUM(col)返回指定列的所有值之和
GROUP_CONCAT(col) 返回由属于一组的列值连接组合而成的结果
1. 查询总员工数:没有分组,默认整体一组
2. 查看每个部门的员工数
3. 查看teacher部门的员工数
4. 查询岗位名以及岗位包含的所有员工名字
5. 查询岗位名以及各岗位内包含的员工个数
6. 查询公司内男员工和女员工的个数
7. 查询岗位名以及各岗位的平均薪资
8. 查询岗位名以及各岗位的最高薪资
9. 查询岗位名以及各岗位的最低薪资
10. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
练习
1、select count(id) from employee;
2、select post,count(id) from employee group by post;
3、select count(id) from employee where post='teacher';
4、select post,group_concat(name) from employee group by post;
5、select post,count(id) from employee group by post;
6、select sex,count(id) from employee group by sex;
7、select post,avg(salary) from employee group by post;
8、select post,max(salary) from employee group by post;
9、select post,min(salary) from employee group by post;
10、select sex,avg(salary) from employee group by sex;
查询结果
七、having过滤条件
having与where不一样的地方在于!!!
!!!执行优先级从高到低:where > group by > having
1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
where可以:select * from employee where salary > 10000;
hanving不可以:
mysql> select post from employee group by post;
ERROR 1046 (3D000): No database selected
mysql> select * from employee having salary > 10000;
ERROR 1046 (3D000): No database selected
1、取出员工数大于3的部门
2、取出id是大于10的,员工数大于3的部门
3、查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
4、查询各岗位平均薪资大于10000的岗位名、平均工资
5、查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
练习
1、select post from employee group by post having count(id) > 3;
2、select post from employee where id > 10 group by post having count(id) > 3;
3、select post,group_concat(name),count(id) from employee group by post having count(id) < 2;
4、select post,avg(salary) from employee group by post having avg(salary) > 10000;
5、select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000;
查询结果
八、distinct :去重
查出公司都有哪些办公室,部门编号和岗位。
mysql> select distinct office,depart_id,post from employee;
+--------+-----------+----------------------------+
| office | depart_id | post |
+--------+-----------+----------------------------+
| 401 | 1 | 老男孩驻沙河办事处外交大使 |
| 401 | 1 | teacher |
| 402 | 2 | sale |
| 403 | 3 | operation |
+--------+-----------+----------------------------+
4 rows in set (0.00 sec)
九、查询排序:order by
默认是asc排序(升序),desc排序(降序)
按单列排序
SELECT * FROM employee ORDER BY salary;
SELECT * FROM employee ORDER BY salary ASC;
SELECT * FROM employee ORDER BY salary DESC;
按多列排序:先按照age排序,如果年纪相同,则按照薪资排序
SELECT * from employee ORDER BY age,salary DESC;
1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
练习
1、select * from employee ORDER BY age asc,hire_date desc;
2、select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) asc;
3、select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc;
查询结果
十、limit限制条数
查出公司的前三个员工的信息
mysql> select * from employee limit 3; #默认从头开始
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)
查出公司工资最高的3个人信息
mysql> select * from employee order by salary desc limit 3;
+----+--------+------+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+------+-----+------------+-----------+--------------+------------+--------+-----------+
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
+----+--------+------+-----+------------+-----------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)
分页显示,一页显示三个
mysql> select * from employee limit 0,3; #3,3 #6,3
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)
查出公司从第七个员工开始的后三个员工信息
mysql> select * from employee limit 6,3;
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
十一、使用正则表达式查询
^ 行首,开头
$ 行尾,结尾
. 除了换行符以外的任意单个字符
* 前导字符的零个或多个
.* 所有字符
[] 字符组内的任一字符
[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符)
^[^] 非字符组内的字符开头的行
[a-z] 小写字母
[A-Z] 大写字母
[a-Z] 小写和大写字母
[0-9] 数字
\<</font> 单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
\> 单词尾
x+:左边的那一个字符有1个到无穷个
x?:左边的那一个字符有0个到1个
x{n}:左边的那一个字符有n个
x{n,m}:左边的那一个字符有n个到m个
x{n,}:左边的那x一个字符有n个到无穷个
| 或
查出公司以a开头的员工信息
mysql> select * from employee where name regexp '^a';
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec)
查出公司以n结尾的员工姓名
mysql> select name from employee where name regexp 'n$';
+--------+
| name |
+--------+
| egon |
| jinxin |
+--------+
2 rows in set (0.00 sec)
查看所有员工中名字是jin开头,n或者g结果的员工信息
mysql> select * from employee where name regexp 'n$';
+----+--------+------+-----+------------+----------------------------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+------+-----+------------+----------------------------+--------------+----------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
+----+--------+------+-----+------------+----------------------------+--------------+----------+--------+-----------+
2 rows in set (0.00 sec)