当前位置:首页 > 技术文章 > 正文内容

小姐姐带你学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;


扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/4345.html

标签: sql怎么学
分享给朋友:

“小姐姐带你学SQL(零基础学sql视频教程)” 的相关文章

K8S中Service使用nginx控制器实现Ingress负载均衡器的一个Demo

写在前面学习K8s中Service遇到,单独整理分享给小伙伴本文内容涉及:ingress-nginx-controller的创建基于ingress-nginx-controller的Ingress的创建基于Ingress的服务发布,SVC负载时间关系,关于Ingresshttp路由负载本文没有涉及部...

Nginx 在微服务中的应用(9)

在微服务架构中,Nginx 通常作为反向代理服务器、负载均衡器和 API 网关,承担着多个关键角色。它可以帮助管理微服务之间的流量,确保请求的高效路由、负载均衡、流量控制、安全防护等。Nginx 由于其高性能、轻量级和配置灵活性,成为微服务架构中非常常见的组件。以下是 Nginx 在微服务中的几种主...

nginx开启ssl并把http重定向到https的两种方式

1 简介Nginx是一个非常强大和流行的高性能Web服务器。本文讲解Nginx如何整合https并将http重定向到https。https相关文章如下:(1)Springboot整合https原来这么简单(2)HTTPS之密钥知识与密钥工具Keytool和Keystore-Explorer(3)Sp...

Nginx热升级流程,看这篇就够了

在之前做过 Nginx 热升级的演示,他能保证nginx在不停止服务的情况下更换他的 binary 文件,这个功能非常有用,但我们在执行 Nginx 的 binary 文件升级过程中,还是会遇到很多问题,比如老的 worker 进程一直退不掉或者新的 worker 进程升级以后出现问题需要考虑回滚,...

安装Code-server并配置用于多用户的反向代理(Nginx)

1.安装code-servercode-server的项目网址在cdr/code-server.在code-server的README中, 提供了两份部署指南, 一份是用于部署在AWS等云服务的deploy, 另一份则是我们要用的用于部署在本地的quickstart.Guide非常简洁明了, 就三步...

给你的Nginx加个防火墙

引言朋友的一个 WordPress 站经常访问慢。看了一下日志,发现整天被扫描网站目录,如phpmyadmin 或者 SQL 文件,和被 CC攻击。找了一下,发现 ngx_lua_waf 是个不错的方案,但是太久不更新了,而且代码我看不懂,猝最后找到 oneinstack 一键包内置的 ngx_lu...