🙂🙂
- 语法
- 例子
- 所有查询的综合叠加使用
- 所有的汉字都要加单引号
- 不要忘记逗号
- where可以在多数查询条件中使用
select数据查询----表
【1】筛选列
select 列名 from 表名 //列名可以多个,且*表示查询全部
use 数据库名称 go select 列名 from 表名
select 列名 as 中文名 from 表名 select 列名 as 中文名 from 表名(不要as也可)
【2】where简单查询
- 比较运算符
- 逻辑运算符
- 集合运算符
🙂
字段 between 数值1 and 数值2 在筛选列的基础上,where等运算符使用叠加 select sno 学号,sname 姓名,ssex 性别 from student where ssex in('女') //where比较常用而已
【3】top-n/distinct/排序的查询
🙂
//TOP-N select top n 字段 from 表
//distinct select distinct 字段 from 表 --distinct 清除多余的行 --select count(*)人数 from student --select count(distinct(sno)) from student
select 字段,classno 字段 from 表 order by 字段 asc,字段 desc
【4】常用内置函数
常用日期函数
🙂
select datediff(day/mouth/year,'日期','日期') //日期:年/月/日,用-
常用的字符串函数
🙂
select * from student where left(sname,1)='徐' and len(sname)=2 -----查询两个字姓徐的全部同学 --select ltrim(classno) 班级号 from student --select rtrim(classno) 班级号 from student select substring('abcdefg',3,2)--从第三个字符开始的数两个字符 --select str(year(birth)) from student
【5】模糊查询
🙂
select * from student where sname not like'李%'//用通配符筛选
【6】表数据操作——增/删/改
插入
--插入完整数据 --insert into student --values(7777777777,'李四','男','计算机22205') --select * from student --插入不完整数据 --方法1 --insert into student --values(7777777777,'张三',null,null) ----方法2 --insert into student --(sno,sname)--写出你要插入数据的列名 --values(7777777777,'张三') --select * from student
更新
--更新 --select * from SCORES --update SCORES --set grade=grade+2
删除
--删除 delete from student where sname like '张%' select * from student where sname like '张%'
【7】数据汇总
聚合
这里主要应用就是用【聚合函数】去处理数据。
🙂
--数据汇总 --查询学习了大学英语的人数 --select count(*)人数, --avg(grade)平均成绩,max(grade)最高成绩, --min(grade)最低成绩,sum(grade)成绩总和, --stdev(grade)成绩标准差,var(grade)成绩方差 from SCORES --where course='大学英语'
分类
🙂
--分组汇总 --select * from student --统计各个班的学生人数 --select classno 班级号,count(*)人数 from student --group by classno --统计某个班的学生人数 --select classno 班级号,count(*) 人数 from student --group by classno --having classno='多媒体06101' or classno='多媒体06101' --或者 --select classno 班级号,count(*) 人数 from student --where classno='多媒体06101' or classno='多媒体06101' --group by classno --统计男生人数多余7的人数 --select classno 班级号,count(*) 人数 from student --where ssex='男'--先把男的人数搞出来每个班 --group by classno --having count(*)>3---大于3 --统计各个班的男女生人数 --select classno 班级号,count(*) 人数,ssex from student --where ssex='男'or ssex='女' --group by classno,ssex
感谢大家,有补充可以在评论区留言!当然因为我们学校期末考试很水,所以以上这些足够应付期末考试,希望大家可以结合自己的情况好好复习!!