SqlServer--查询案例

时间:2023-03-09 08:10:33
SqlServer--查询案例

use  MyDataBase1

-- * 表示显示所有列

-- 查询语句没有加where条件表示查询所有行

select *from TblStudent

---只查询表中的部分列

select tsid,tsname,tsgender from TblStudent

--根据条件,只查询部分行(使用where条件筛选部分行显示)

select * from TblStudent where tsclassId=5

--为查询结果集中的列起别名

select tsid as 学生编号,tsname as 学生姓名,tsgender as 性别 from TblStudent

--也可以这样排列

select

tsid as 学生编号,

tsname as 学生姓名,

tsgender as 性别

from TblStudent

select

tsid '(学生 编号)', --可以输出空格

tsname 学生 姓名, --不能输出空格

tsgender 性别

from TblStudent

select

学生编号=tsid,

学生姓名=tsname,

性别=tsgender

from TblStudent

select

学生编号=tsid,

学生姓名=tsname,

性别=tsgender,

婚否='否'

from TblStudent

--并不是说select必须配合from一起来使用,可以单独使用select

select

当前系统时间=getdate()

select

班长='严蒙',

班花='待定',

班草='待定',

班主任='宋词'

--distinct关键字,针对已经查询出的结果然后去除重复

select distinct * from TblStudent

select distinct tsname,tsgender,tsaddress from TblStudent