小姐姐带你学SQL(零基础学sql视频教程)
--------------------------------------------第一天
1、创建数据库
格式:
create database 数据库名;
例子:创建一个名为“dt_db”的数据库。
create database dt_db;
2、删除数据库
格式:
drop database 数据库名;
例子:删除一个名为“dt_db”的数据库。
drop database dt_db;
3、创建一个数据库表
格式:
create table 表名(字段1 字段1类型,字段2 字段2类型,......,字段n 字段n类型);
例子:创建一个名为“student”的数据库表,其中包括字段:ID,name,age,classroom。
create table student(ID int(11),name varchar(20),age int(3),classroom varchar(25));
4、删除数据库表
格式:
drop table 表名;
例子:
drop table student;
--------------------------------------------第二天
5、向数据库表中插入数据
格式
insert into 表名(字段1,字段2,字段3,...,字段n) values (内容1,内容2,内容3,...,内容n);
例子:
insert into student(ID,name,age,classroom) values (101,'黄小花',14,'初三(10)班');
6、查询数据库表的所有数据
格式
select * from 表名;
例子
select * from student;
7、单条件查询
格式
select * from 表名 where 字段=内容;
例子
select * from student where name='黄小花';
8、多条件and查询
格式
select * from 表名 where 字段1=内容 and 字段2=内容 and ....字段n=内容;
例子
select * from student where name='黄小花' and classroom='初三(10)班';
9、或(or)条件查询
格式
select * from 表名 where 字段1=内容 or 字段2=内容;
例子
select * from student where name='黄小花' or classroom='初三(10)班';
10、固定列查询
格式
select 字段1,字段2...字段n from 表名;
例子
select name,age from student;
11、对查询结果排序(order by)
格式
select * from 表名 order by 字段1,字段2...[desc或者asc];
例子
select * from student order by age desc;
select * from student order by age;
12、模糊查询(like、not like)
格式
select * from 表名 where 字段1 like '%内容%';
select * from 表名 where 字段1 not like '%内容%';
例子
select * from student where name like '黄%';
select * from student where name not like '黄%';
------------------------------------------第三天
13、in与not in
格式
select * from 表名 where 字段 in ('内容1','内容2','内容3','内容4');
select * from 表名 where 字段 not in ('内容1','内容2','内容3','内容4');
例子
select * from stundet where ID in ('1','2','3');
select * from stundet where ID not in ('1','2','3');
14、between....and....
格式
select * from 表名 where 字段 between 数值1 and 数值2;(只对数值有效)
例子
select * from student where id between 1 and 3;
15、更新数据
格式
update 表名 set 字段1=内容1,字段2=内容2.... where....;
例子
update student set classroom='初二(1)班' where id='1';
16、删除数据
格式
delete from 表名 where ....;
例子
delete from student where name='黄小花';
----------------------------------------第四天
17、多表关联查询
格式
select * from 表A,表B where A.字段1=B.字段1;
例子
select * from student_info a,grade_info b where a.code=b.studebt_code;
18、inner join
格式
select * from 表A inner join 表B on A.字段1=B.字段1;
例子
select * from student_info a inner join grade_info b on a.code=b.studebt_code;
19、left join
格式
select * from 表A left join 表B on A.字段1=B.字段1;
例子
select * from student_info a left join grade_info b on a.code=b.studebt_code;
20、right join
格式
select * from 表A right join 表B on A.字段1=B.字段1;
例子
select * from student_info a right join grade_info b on a.code=b.studebt_code;
21、计算行记录count()函数
格式
select count(1) from 表A where 。。。。;
例子
select count(1) from grade_info where student_code='330601';
22、求最大值max()
格式
select max(字段A) from 表A where 。。。。;
例子
select max(score) from grade_info where student_code='330601';
23、求最大值min()
格式
select min(字段A) from 表A where 。。。。;
例子
select min(score) from grade_info where student_code='330601';
24、求平均值avg()
格式
select avg(字段) from 表A where ... ;
例子
select avg(score) from grade_info where student_code='330601';
25、分组函数 group by
例子
select 字段1,字段2,count(1) from 表A where ... group by 字段1,字段2;
select 字段1,字段2,max(字段) from 表A where ... group by 字段1,字段2;
select 字段1,字段2,min(字段) from 表A where ... group by 字段1,字段2;
例子
select student_code,count(1) from grade_info where student_code='330601' group by student_code;
select student_code,max(score) from grade_info where student_code='330601' group by student_code;
select student_code,min(score) from grade_info where student_code='330601' group by student_code;
26、保留小数位数 round()
格式
select round(字段,2) from 表;
例子
select round(score,0) from grade_info;
27、求和sum()
格式
select 字段1,sum(字段) from 表 group by 字段1;
例子
select student_id,sum(score) from grade_info group by student_id;
28、表合并union与union all
格式:union 会去重,union all 不会
select 字段1,字段2,字段3,..... from 表A
union
select 字段1,字段2,字段3,..... from 表B;
select 字段1,字段2,字段3,..... from 表A
union all
select 字段1,字段2,字段3,..... from 表B;
例子
select code,name from student_info
union
select student_id,subject from grade_info;
28、拼接字符串concat(str1,str2,....)
格式
select concat(字段1,字段2) from 表;
例子
select concat(code,name) from student_info;
29、字符长度计算length(str)
格式
select length(字段) from 表;
例子
select length(name) from student_info;
30、字符替换replace(字符串,原字符,目标字符)
格式
select replace(字段,原字符串,目标字符串) from 表;
例子
select replace(classroom,'1','2') from student_info;