數(shù)據(jù)庫(kù)簡(jiǎn)單查詢筆記
1.select后面永遠(yuǎn)是字段
2.查找一張表里面的全部信息
select *
3.查詢姓張王李趙的男生
我的做法:
select *
from dbo.Student
where Name like '張%'and SEX ='男'
select *
from Student
where Name like '王%'and SEX ='男'
select *
from Student
where Name like '李%'and SEX ='男'
select *
from Student
where Name like '趙%'and SEX ='男'
正確答案的做法:
'[張王李趙]%'
4.限制取值? ?查詢課程編號(hào)為101,102,103三門課程的課程號(hào)和課程名。
select CourseID ,CourseName
from Course
where CourseID IN('101','102','103')
5.消除結(jié)果中的重復(fù)行:DISTINCT
限制結(jié)果返回行數(shù):TOP: select top %50 NAME,SEX
6.查詢1996至2000年出生的學(xué)生學(xué)號(hào),姓名,和出生日期。
select StudentID,Name,Birthday
?from Student
?where Birthday between '1996' and '2000'
7.根據(jù)學(xué)生出生日期,將學(xué)生分為:低齡(17歲以下),正常(17至30),高齡(30歲以上),顯示學(xué)號(hào),姓名,出生日期,年齡段4列。
?use college
?go
select StudentID,Name,Birthday,RANK=
CASE?
? ?when Birthday>'2004' then'低齡'
? ?when? Birthday between '1992' and '2004' then'中齡'
? ?else'高齡'
? ?end
? ?from Student
? ?go
8.根據(jù)學(xué)生的出生日期,生成年齡列。
?use college?
?go
?select Name,YEAR(GETDATE())-YEAR(Birthday) AS '年齡'
?from Student?
?go