/***************************** 數據庫查詢 select 2017-7-21 09:02:02 **********************/
-- 查詢語句--學習目標
-- 模糊查詢
-- 通配符
-- % 任意長度字符串
-- _ 任意一個字符
-- [] 括號中所指範圍內的一個字符
-- [^] 不在括號中所指範圍內的一個字符
-- Like / in 進行模糊查詢
-- 當對查詢字段沒有準確描述,只給出了部分時候就需要使用模糊查詢
-- like
-- select * from table_name where 字段 like 條件
-- select * from table_name where name like '張%'
-- in
-- 只需滿足多個條件中的一個查詢條件時候就需要用in運算符
-- select 字段 from table_name where 字段 in ()
-- 聚合函數
-- max avg min count
-- 分組查詢
-- 使用group by對數據進行分組查詢
/****************************************************************/
use studentSys
select * from studentTest
update studentTest set studentName = '張氏' where studentId = 2
-- 模糊查詢
-- like %
-- 查詢姓趙的人
select * from studentTest where studentName like '李%'
-- 查詢姓名中帶有c的人名
select * from studentTest where studentName like '%c%'
-- like _
-- 查詢第五個字符是c的人名
select * from studentTest where studentName like '____c%'
-- like []
-- 查詢以c或者是b開頭的人信息
select * from studentTest where studentName like '[cb]%'
-- 查詢姓陳魯小的人信息
select * from studentTest where studentName like '[陳魯小]%'
-- like [^]
-- 查詢不是以c或b開頭的人信息
select * from studentTest where studentName like '[^cb]%'
-- 查詢不是姓張李
select * from studentTest where studentName like '[^張李]%'
-- 查詢人名爲_的人信息
select * from studentTest where studentName like '%[_]%'
-- ********************** in **************************
-- 查詢上海和武漢的學生(in 的情況完全可以用or替換,in並且可以使用子查詢)
select * from studentTest where studentAddress in ('武漢','上海')
select * from studentTest where studentAddress = '武漢' or studentAddress = '上海'
-- ******************** between ************************
-- 在where子句當中,可以使用between運算符在兩個值之間進行篩選
-- select 字段 from table_name where 字段 between 值1 and 值2
-- 值1 <= 值2
-- between 可以用and替換掉
-- 用於數字類型的篩選
--用於時間日期類型的篩選
-- 查詢學號在3到6之間的
select * from studentTest where studentId >=3 and studentId <= 6
select * from studentTest where studentId between 3 and 6
-- select * from studentTest where studentId between 6 and 3 (between and 的兩個值的順序)
-- 查詢學生的出生年日範圍(出生日期在1996-1-1 到 1996-12-31之間的)
select * from studentTest where studentBorDay between '1996-1-1' and '1996-12-31'
-- ******************** 聚合函數(使用student score course classes表) ***********************
-- sum 求和
-- max 最大值
-- min 最小值
-- avg 平均值 (一列當中非null的平均值)
-- count 求總數 (統計個數)
-- 使用了聚合函數的列只能夠在聚合函數中,不能夠包含其他列
-- 聚合函數不能夠最爲where子句的直接搜索條件
-- 成績綜合(Java課程分數綜合)
select SUM(scores)as 'Java總分' from Score where courseId = (select courseId from Course where courseName = 'Java')
-- java課程最高分
select MAX(scores) as 'Java最高分' from Score where courseId = 1
-- Java平均分
select AVG(scores) as 'Java平均分' from Score where courseId = 1
-- java最低分
select MIN(scores) as 'Java最低分' from Score where courseId = 1
-- 學習Java的人數
select COUNT(scores) as '學習Java人數' from Score where courseId = 1
-- 查詢Java課程最高分的學號
-- 選擇列表中的列 'Score.stuId' 無效,因爲該列沒有包含在聚合函數或 GROUP BY 子句中。
-- select MAX(scores),stuId from Score where courseId = 1
select MAX(scores),MIN(scores) from Score where courseId = 1
-- ********************** group by 分組查詢 ****************************
-- select 字符 from table_name group by 字段1 字段2
-- null值時候會將所有的null作爲一組
-- 求出每一門課的最高分
select courseId as '課程ID' ,MAX(scores) as '該課程最高分' from Score group by courseId
-- 大於九十分
select courseId as '課程ID' ,MAX(scores) as '課程最高分' from Score where scores > 90 group by courseId
-- 平均分大於80
select courseId as '課程ID' ,AVG(scores) as '課程分數' from Score group by courseId having avg(scores) > 80
/****************************************************************
having 和 where 區別
-- 執行順序的不同
-- where 分組之前 having 分組之後使用
-- having 必須和group by 一起使用,放在group by之後
-- where存在的話放在 group by之前
-- having 之後可以使用聚合函數,where後面不行
-- having 後面使用聚合函數 或者group by分組字段
*/
-- ********************** 查詢語法 ****************************************
-- select 字段 from table_name where 條件 group by 字段 having 條件(聚合函數或者group by 分組字字段) order by 字段 規則
sql server 的T-SQL 學習筆記(六)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.