目錄
select數據查詢----表
【1】篩選列
【2】where簡單查詢
【3】top-n/distinct/排序的查詢
【4】常用內置函數
常用日期函數
常用的字符串函數?
【5】模糊查詢
【6】表數據操作——增/刪/改
插入
更新
刪除
【7】數據匯總
聚合?
分類??
🙂🙂
- 語法
- 例子
- 所有查詢的綜合疊加使用
- 所有的漢字都要加單引號
- 不要忘記逗號
- 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 studentselect 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
感謝大家,有補充可以在評論區留言!當然因為我們學校期末考試很水,所以以上這些足夠應付期末考試,希望大家可以結合自己的情況好好復習!!