跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

【数据库】SQL语句(超全查询语句)

SQL (structured query language)结构化查询语言,可以使用sql去操作oracle,sql server,mysql,sqlite等所有的关系型的数据库。

MySQL是一个关系型数据库管理系统,支持多操作系统。

SQL语句 注释快捷键: ctrl + / 取消注释快捷键: ctrl + shift + / 1.对表的操作 创建表 :

create table 表名(

字段名 类型 约束(不是必填选项), 字段名 类型 约束, ... )

例如:

--创建一个学生表

create table `students`( `student_id` unsigned primary key auto increment, --设置主键(唯一标识符,相当于人的身份证号,自动递增) `name` varchar(10) , `age` int unsigned, --无符号整数 `height` decimal(5,2), --小数点后面保留两位,整数为5-2=3位 `sex` varchar(10), `hometown` varchar(10) )

此问题是 MySql 语法上的错误,在 MySQL 中,为了区分 MySQL 的关键字与普通字符,MySQL 引入了一个反引号。

在上述的 sql 语句中,列名称没有使用反引号或者列名称使用单引号,都会报这个错误出来。

删除表

--格式一:

drop table 表名 --表存在就删除,表不存在就报错 --格式二: drop table if exists 表名 --(表不存在就不删,不会报错,创建表之前一般会填上这条语句)

2.对表中数据进行操作 插入数据 直接插入数据 给所有字段设置数据: insert into 表名 value(…)

--在学生表里插入一个学生,设置所有字段的信息

--值必须与表中的字段匹配 insert into student(default,'小明',20,161.2,'男','北京',)

给指定的字段设置数据: inster into 表名(字段名1,字段名2…) values (…)

inster into students(name,age) values ('花花',11)

插入多条数据 (1) 写多条插入语句:

insert into student(student_id,name,age,height,sex,hometown) values (default,'安琪拉',21,181.2,'女','北京');

--字段名可省略,直接插入 insert into student values (default,'小乔',10,171.2,'女','北京'); insert into student values (default,'亚索',11,163.2,'男','上海')

(2) 一条插入语句插入多个数据

insert into student values (default,'安琪拉',21,181.2),(default,'小乔',10,171.2),(default,'亚索',11,163.2);

insert into student(name) values ('花花'),('李明')

修改数据 update 表名 set 字段名=要改的值 where 要改的字段

--改一个字段

update student set name='狄仁杰' where name='花花' ; --改多个字段 update student set name='狄仁杰',age=13 where name='花花'

删除数据 (1)delete from 表名 where … delete删除的数据无法恢复

--删除student_id=6的学生信息

delete from student where studet_id=6

(2)逻辑删除 设置一个isdelete标志,默认值为0 当想删除一条信息时,将isdelete值修改成1

--逻辑删除

--1.设计表,给表添加一个字段iddelete,1代表删除,0代表没有删除 --2.把所有的数据的isdelete都设为0 --3.要删除某一数据时,将该条数据的isdelete改成1(使用update 表名 set isdelete=1 where xx=xx) --4.当要查询数据时,只查询isdelete为0的数据

查询数据 基本查询 select 字段名1,字段名2…from 表名(显示表的列,当字段名部分为*号时,查询所有) select 字段名 as 别名 from 表名(不会改变表中的数据,只是改变查询结果中的字段名) 去重查询 select distinct 字段名 from 表名

-- age列去重查询

select distinct age from student -- age,height两列去重查询(两个参数都要相同) select distinct age,height from student

条件查询 select 字段名 from 表名 where 条件(select 用来过滤列,where用来过滤行)

--在学生表中查询student_id=1的学生姓名

select name from student where student_id=1 --在学生表中查询年龄小于20的学生 select *from student where age<20 --在学生表中查询年龄小于20的女学生的姓名 select name from student where age<20 and sex!='男'

模糊查询(like% / _) select 字段名 from student where 字段名 like ’ '(%代表任意字符, " _ "代表一个字符)

--在学生表中查询姓李且名字为一个字的学生姓名

select * from student where name like '李_' --在学生表中查询名字以王结尾的学生id select id from student where name like '%王' --在学生表中查询姓名含'勇'的学生 select * from student where name like '%勇%' --在学生表中查询姓名为两个字的学生且年龄大于10的学生 select * from student where name like '__' and age>10

范围查询(in) (1) 关键字IN :(适用于字符串) select 字段名1 from 表名 where 字段名2 in () (2)关键字Between… and…:(适用于数字) select 字段名1 from 表名 where 字段名2 between a and b

--在学生表中查询家乡在北京或上海或广东的学生

--使用逻辑查询 select * from student where hometown ='北京',hometown ='上海',hometown ='广东' --使用范围查询(in) select * from student where hometown in ('北京','上海','广东') --在学生表中查询年龄在10-12的学生 --使用逻辑查询 select * from student where age>=10 and age<=12 --范围查询(between..and..) select * from student where age between 10 and 12 --在学生表中查询年龄在10-12之外的学生 select * from student where not age between 10 and 12

空判断 Null!=空字符串

--查询没有填写家乡的学生

select *from student hometown is not

排序

--基本格式:

select * from 表名 order by 列(字段名)asc|desc....(asc为升序,desc为降序)

具体实例:

--查询所有学生信息,按年龄从小到大的顺序查看

select * from student order by age asc --查询所有学生信息,按年龄从大到小的顺序查看,年龄相同时,再按id从小到大排序 select * from student order by age desc ,id asc --排序时优先按照第一个字段排序,当第一个字段相同时,会在此基础上按照第二个字段进行排序 --当需要对中文进行排序时,需要先进行编码转换 --查询所有学生信息,按照姓氏升序排列 select * from student order by convert(name using gbk)

聚合函数(用于统计数据)

--统计总数

select count(*) from 表名 --查询最大值 select max(列) from 表名 --查询最小值 select min(列) from 表名 --计算平均值 select avg(列) from 表名

--count(*)表示计算总行数,一般不会count某一字段

--查询学生总数 select count(*) from student; --max(列)查询列最大值 --查询女生的最大年龄 select max(age) from student where sex='女' --min(列)查询列最小值 --查询最小的北京学生 select min(age) from student where hometown = '北京' --sum(列)列求和 --查询广东学生的年龄总和 select sum(age) from student where hometown='北京' --avg(列)求列平均值 --查询男生的平均年龄 select avg(age) from student where sex='男' --查询所有学生的最大年龄,最小年龄,平均年龄 select from max(age) as 最大年龄,min(age) as 最小年龄, avg(age) as 平均年龄 student

分组 group by 按照某一字段进行分组,将相同的值分到一个组里, 分组一般结合聚合函数来使用

基本格式

select * from 表名 group by 列

--查询各性别的人数(按照一个字段进行分组)

select sex,count(*) from student group by sex --查询各种年龄的人数(可以去重) select age,count(*) from student group by age --查询各个地区的平均年龄和最小年龄 select hometown,avg(age) as 平均年龄,min(age) as 最小年龄from student group by hometown --查询各个地区的男生总人数和女生总人数(按照多个字段进行分组,多个字段的值相同才会分到一个组里) select hometown,sex count(*) from student group by hometown,sex

分组过滤 having必须跟在group by 后面 ,先分组再过滤

基本格式

select * from 表名 group by 列名 having 列名

--查询男生总人数

--使用count直接实现 select count(*) as 男生总人数 from student where sex ='男' --使用分组过滤来实现 select sex count(*) from student group by sex having sex='男' --查询北京以外其它地区学生的最大年龄,最小年龄,平均年龄 --where实现(where是对from后面指定的表进行数据筛选,属于原始数据的筛选) select hometown,max(age),min(age),avg(age) from students where hometown!='北京' group by hometown --分组过滤实现(having是对group by的结果进行筛选) select hometown,max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from student group by hometown having hometown!='北京' --查询北京的男女各多少人 select sex,count(*) from student where hometown= '北京' group by sex

获取部分行 当数据量过大时,在一页中查询数据会非常麻烦, 需要获取部分行查询

--基本格式

select * from 表名 limit start,count --从start开始,获取count条数据 --start索引从0开始

实例:

--查询前三条学生信息

select * from student limit 0,3 --查询年龄第三大的学生信息 select * from student order by age limit 1,1 --查询年龄最大的学生信息 select * from student order by age desc limit 1

分页

--分页查询,要求每页显示3条

--1.先获取数据库中总条数,计算页数 select count(*) from student --12/3共四页 --2.第一页 select * from student limit 0,3 --3.第二页 select * from student limit 3,3 -- 4.第三页 select * from student limit 6,3 -- 5.第四页 select * from student limit 9,3

分页总结(假设每页a条数据): 1.首先得到表中的数据总条数s, 计算出总共分多少页 s/a,需要写s/a条sql语句 2.利用limit语句来设置每一页[套用公式star=a*(第几页-1), count = a ] 连接查询(多表查询) 学生表student中的字段 | student_id | name | height| sex| hometown | 成绩表score中的字段 |id | student_id | course_id | | score | 课程表crouse中的字段 | course_id | name (1)等值连接

--格式一:

select * from 表1,表2 where 表1.列1= 表2.列2 [先连接表,再通过where过滤] 此方法会产生临时表,一般不用 --格式二(内连接) select * from 表1 inner join 表2 on 表1.列=表2.列[先判断过滤条件,再进行连接]

--查询学生信息及学生成绩

select * from student,score where student.student_id = score.student_id --使用别名 select * from student as stu ,score as sc where stu.student_id = sc.student_id

(2)内连接(inner join on)

--格式(内连接):

select * from student inner join scores on student.student_id = score.stuedent_id

-- 查询学生信息及学生的课程对应的成绩

--需要用到三个表 --方法一: select * from student student, courses, scores, where student.stuedent_id=scroes.student_id and scores.course_id =course.course_id

--格式二:

select * from student inner join score on student.stuedent_id=scroes.student_id inner join course on scores.course_id =course.course_id

--连接查询后再过滤

--表中有重复的字段,需要指出(不能直接查询name字段,因为学生表和课程表中都要name字段,需要指出要查哪个表中的name)

--查询小明的语文成绩,要求显示姓名、课程名、成绩

--方法一 select student.name, course.name, score from student,courses,scores, where student.stuedent_id=scroes.student_id and scores.course_id =course.course_id and student.name ='小明' and course.name = '语文'

--方法二 select student.name from student inner join score on student.stuedent_id=scroes.student_id inner join course on scores.course_id =course.course_id where student.name ='小明' and course.name = '语文'

自关联 自关联查询 对一个表查询多次,但需要给表名起别名

select from 表a as a,表a as b

where ....

子查询 有多个select语句 (1)标量子查询(返回结果为一个值)

--查询大于平均年龄的学生(括号括起来的是子查询,先查)

select * from student where age>(select avg(age) from stuent) --查询年龄最小的学生 select * from student where age=(select min(age) from student) --查询小明的成绩 select score from score where student_id =(select student_id from student where name ='小明') --查询小明的数据库成绩 select score from score where student_id =(select student_id from student where name ='小明') and course_id =(select course_id from course where name='数据库')

(2)列子查询(子查询返回的结果为一列)

--查询18岁的学生的成绩,要求显示成绩

select score from score where student_id in (select student_id from student where age =18)

(3)行子查询(不常用) (4)表子查询

--查询语文和数学成绩

select * from score inner join --把查询出来的结果当作数据源使用,必须起个别名 (select * from coruse where name in ('数学,语文) as c ) on score.course_id = c.course_id

子查询特定关键词的使用: any = some = in any、some(要搭配<>=来使用) 例如 a>=any(b,c,c) 返回大于等于b,c中 的任意一个

相关文章