表记录的删改查

关于表的操作:

插入数据

1. 插入完整数据(顺序插入)
语法一
insert into 表名(字段1,字段2,字段3...) values(值1,值2,值3...); #指定字段来插入数据,插入的值要和你前面的字段相匹配
语法二
insert into 表名 values(值1,值2,值3...);#不指定字段的话,就按照默认的几个字段来插入数据
2. 指定字段插入数据
语法:
insert into 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);
3. 插入多条记录
    语法:#插入多条记录用逗号来分隔
    insert into 表名 values
        (值1,值2,值3…值n),
        (值1,值2,值3…值n),
        (值1,值2,值3…值n);
4. 插入查询结果
    语法:
    insert into  表名(字段1,字段2,字段3…字段n) 
                    select (字段1,字段2,字段3…字段n) from 表2
                    where …; #将从表2里面查询出来的结果来插入到我们的表中,但是注意查询出来的数据要和我们前面指定的字段要对应好

更新数据

语法
upadte 表名 set 
    字段1=值1,  #注意语法,可以同时来修改多个值,用逗号分隔
        字段2=值2,
        where condition; #更改哪些数据,通过where条件来定位到符合条件的数据
update mysql.user set password=password("123")
    where user=’root’ and host=’localhost’;

删除数据

语法:
delete from 表名
    where conition; #删除符合条件的一些记录
示例
delete from mysql.user 
    where password="123";

查询数据(单表查询)

语法:
select * from, 这个select * 指的是要查询所有字段的数据
select distinct 字段1,字段2... from 库名.表名
       where 条件
        group by field(字段)
        having 筛选
        order by field(字段)
        limit 限制条数
关键字的执行优先级
from
where
group by
having
select
distinct
order by
limit

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
 1 #创建表
 2 create table employee(
 3     id int not null unique auto_increment,
 4     name varchar(20) not null,
 5     sex enum('male','female') not null default 'male', #大部分是男的
 6     age int(3) unsigned not null default 28,
 7     hire_date date not null,
 8     post varchar(50),
 9     post_comment varchar(100),
10     salary double(15,2),
11     office int, #一个部门一个屋子
12     depart_id int
13 );
14 #插入记录
15 #三个部门:教学,销售,运营
16 insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
17 ('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部,全都是老师
18 ('alex','male',78,'20150302','teacher',1000000.31,401,1),
19 ('wupeiqi','male',81,'20130305','teacher',8300,401,1),
20 ('yuanhao','male',73,'20140701','teacher',3500,401,1),
21 ('liwenzhou','male',28,'20121101','teacher',2100,401,1),
22 ('jingliyang','female',18,'20110211','teacher',9000,401,1),
23 ('jinxin','male',18,'19000301','teacher',30000,401,1),
24 ('成龙','male',48,'20101111','teacher',10000,401,1),
25 
26 ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
27 ('丫丫','female',38,'20101101','sale',2000.35,402,2),
28 ('丁丁','female',18,'20110312','sale',1000.37,402,2),
29 ('星星','female',18,'20160513','sale',3000.29,402,2),
30 ('格格','female',28,'20170127','sale',4000.33,402,2),
31 
32 ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
33 ('程咬金','male',18,'19970312','operation',20000,403,3),
34 ('程咬银','female',18,'20130311','operation',19000,403,3),
35 ('程咬铜','male',18,'20150411','operation',18000,403,3),
36 ('程咬铁','female',18,'20140512','operation',17000,403,3)
37 ;
38 
39 #ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
例子
#1:单条件查询
select name from employee
    where post='sale';
#2:多条件查询
select name,salary from employee
    where post='teacher' and salary>10000;
#3:关键字BETWEEN AND 写的是一个区间
select name,salary from employee
    where salary between 10000 and 20000;

select name,salary from employee
    where salary not between 10000 and 20000;
#4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS) 判断null只能用is
select name,post_comment from employee
    where post_comment is null;
#5:关键字IN集合查询
select name,salary from employee
    where salary in (3000,3500,4000,9000);
#6:关键字LIKE模糊查询,模糊匹配,可以结合通配符来使用
通配符’%’  #匹配任意所有字符
select * from employee
    where name like 'eg%';
通配符’_’  #匹配任意一个字符 
select * from employee
    where name like 'al__';

分组查询

解释
#1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的
#2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等

group by

GROUP BY关键字和GROUP_CONCAT()函数一起使用,比如说我想按部门分组,每个组有哪些员工,都显示出来,怎么搞
    SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照岗位分组,并查看组内所有成员名,通过逗号拼接在一起
    SELECT post,GROUP_CONCAT(name,':',salary) as emp_members FROM employee GROUP BY post;
GROUP BY一般都会与聚合函数一起使用,聚合是什么意思:聚合就是将分组的数据聚集到一起,合并起来搞事情,拿到一个最后的结果
    select post,count(id) as count from employee group by post;

关于集合函数,mysql提供了以下几种聚合函数:count、max、min、avg、sum等,上面的group_concat也算是一个聚合函数了,做字符串拼接的操作

聚合函数

#强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组
示例:
    SELECT COUNT(*) FROM employee;  #count是统计个数用的
    SELECT COUNT(*) FROM employee WHERE depart_id=1;  #后面跟where条件的意思是统计一下满足depart_id=1这个的所有记录的个数
    SELECT MAX(salary) FROM employee;  #max()统计分组后每组的最大值,这里没有写group by,那么就是统计整个表中所有记录中薪资最大的,薪资的值
    SELECT MIN(salary) FROM employee;
    SELECT AVG(salary) FROM employee;
    SELECT SUM(salary) FROM employee;
    SELECT SUM(salary) FROM employee WHERE depart_id=3;

去重

distinct

select distinct post from employee; 注意distinct去重要写在查询字段的前面,不然会报错;
有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),
其原因是distinct只能返回他的目标字段,而无法返回其他字段,distinct 想写在其他字段后面需要配合聚合函数来写。 select count(distinct post)
from employee;

查询排序:

order by

按单列排序
    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;

限制查询的记录数:limit  #第一个参数,是索引,第二个是条数

示例:
  #取出工资最高的前三位
    SELECT * FROM employee ORDER BY salary DESC 
        LIMIT 3;                    #默认初始位置为0,从第一条开始顺序取出三条
    SELECT * FROM employee ORDER BY salary DESC
        LIMIT 5,5; #从第5开始,即先查询出第6条,然后包含这一条在内往后查5条

正则表达式查询

#之前我们用like做模糊匹配,只有%和_,局限性比较强,所以我们说一个正则,之前我们是不是学过正则匹配,你之前学的正则表达式都可以用,正则是通用的
SELECT * FROM employee WHERE name REGEXP '^ale';

SELECT * FROM employee WHERE name REGEXP 'on$';

SELECT * FROM employee WHERE name REGEXP 'm{2}';


小结:对字符串匹配的方式
WHERE name = 'egon';
WHERE name LIKE 'yua%';
WHERE name REGEXP 'on$';

查看所有员工中名字是jin开头,n或者g结果的员工信息
select * from employee where name regexp '^jin.*[g|n]$';

猜你喜欢

转载自www.cnblogs.com/lgw1171435560/p/10285592.html