目录
一、DDL操作(数据定义语言)
1、数据库操作
创建数据库:
create database if not exists db1;
销毁数据库:
drop databasae if exists db1;
2、字段操作
alter table tname add 字段名称 类型(长度);//追加字段
3、表格操作
展示所有表格
show tables ;
二、DML操作(数据操纵语言)
1、添加数据
关键字 insert into
如果插入的记录的数据并不完整,可以用如下方式插入:
insert into table1 (fie1,fie2,fie3)
values (v1,v2,v3);
如果插入的记录的数据完整,可以用如下的方式插入:
insert into table1
values (v1,v2,v3,v4~~);
table表示表格的名字。数据的顺序要与表格里字段顺序相同;
如果需要插入多条数据,可以用如下方式:
insert into table1 (f1, f2, f3,f4)
values (v1,v2,v3,v4),
(v1,v2,v3,v4),
(v1,v2,v3,v4),
(v1,v2,v3,v4);
2、修改数据
关键字:update table set ...where continue;
table:表的名字;
set后面是要修改的内容;
where 后面必须是主键信息,(修改条件)
update table1 set f1 = v1 f2 = v2 where id = 1;
3、删除数据
关键字:delete
delete from table1 where id = 1;
where 后面为主键信息。
三、DQL操作(数据查询语言)
1、基本查询
关键字:select f1,f2,f3 from table
1、查询字段列表
select f1,f2,f3,f4.. from table1;
可以在字段f1后面加as' ',在引号里面加入别名。
select f1 as '性别',f2 as '年龄' ,f3 as '身高',f4 as'体重'.. from table1;
2、查询某字段的种类
如职业的种类:
select distinct job from table1;
查询结果为:

2、条件查询
关键字:where +条件
条件关键字:> , >= , < , <= , != , between...and... , in , like , is null ,and , or , not
1、一个条件的查找实例
select * from table1 where name = '小明';
select * from table1 where id <= 5;
select * from table1 where job is null;
select * from table1 where job is not null ;
select * from table1 where password != '123456';
分别为查询指定姓名、id小于等于5、job不为空、job为空、password 不等于 ’123456‘。
2、多个条件的查找
select * from table1 where entrydate >='2000-01-01' and entrydate <= '2010-01-01';
select * from table1 where entrydate between '2000-01-01' and '2010-01-01';
select * from table1 where entrydate between '2000-01-01' and '2010-01-01' and gender = 2;
3、in、like的用法
in: select * from table where job in(2,3,4); //查询job为2,3,4的数据;
like:用于查找姓名,“_”表示任意一个字,“%”表示任意 如下:
select * from table1 where name like '__';//查找名字为两个字的人
select * from table1 where name like '张%';//查找姓张的人
4、聚合函数
-- 求总数
select count(job) from table1;
select count(*) from table1;
--求最大值、最小值
select max(entrydate) from table1;
select min(entrydate) from table1;
-- 求均值、求和
select avg(id) from table1;
select sum(id) from table1;
3、分组查询
关键字:group by、having
示例:
select job,count(*) from table1 where entrydate <= '2015-01-01' group by job having count(*) >= 2;
结果:

1、重点
where 和 having 区别
where:分组之前进行过滤,不参与分组;不能对聚合函数进行判断;
having:分组之后对结果进行过滤;可以对聚合函数进行判断;
4、排序查询
升序ASC,降序DESC
示例:
select *from table1 order by entrydate asc ;
select *from table1 order by entrydate desc ;
select *from table1 order by entrydate ,update_time desc ;
5、分页查询
关键字:limit
limit 起始索引,每页显示记录数
起始索引 = (查询页码 - 1)* 每页显示记录数;
示例:
select *
from table1 limit 0,5;
select *
from table1 limit 5,5;
select *
from table1 limit 10,5;
6、案例
select *
from table1
where name like '%张%'
and gender = 1
and entrydate between '2000-01-01' and '2015-01-01'
order by update_time desc
limit 10,10;

if语句和case语句:
-- if(表达式,ture,fault)
select if(gender = 1, '男性员工', '女性员工') 性别, count(*)
from table1
group by gender;
-- case
select (case job
when 1 then '班主任'
when 2 then '讲师'
when 3 then '学工主管'
when 4 then '教研主管'
else '未分配' end)职位, count(*)
from table1
group by job;
本文详细介绍了SQL中的DDL(数据定义语言)操作,如创建/销毁数据库、字段操作和表格操作;DML(数据操纵语言)涉及插入、修改和删除数据;以及DQL(数据查询语言)包括基本查询、条件查询、聚合函数、分组、排序和分页等实用技巧。
8754





