--选择所有列
select * from educate_plan
--选择指定列并指定显示次序
select teacher,student from educate_plan
--算术运算.如果列值原来为空,则所有的运算执行后仍然为空.数据库的值实际上没有改变
select department=department-1 from educate_plan --department为int型
--指定列标题
select '教师'=teacher from educate_plan
--删除重复行
select distinct teacher,userid from educate_plan
--order by子句.asc表示升序排列,desc表示降序排列
select * from educate_plan order by id desc
--group by子句,通常与集合函数对数据库进行分组统计
select teacher,sum(department) as 汇总 from educate_plan group by teacher
--compute子句,通常与行集合函数对数据库实现对数据库的统计,及显示统计的详细信息
select teacher,department from educate_plan compute avg(department)
--联合查询,主要是用于把两个或以上的查询结果合并成一个结果显示出来
select teacher,department from educate_plan
union
select task,department from workmag
--连接,主要是实现多表查询
--1、等值连接和自然连接
select * from xscj,xsqk where xscj.学号=xsqk.学号
select xscj.*,xsqk.姓名,xsqk.班级 from xscj,xsqk where xscj.学号=xsqk.学号
--2、不等连接与自连接.自连接指的是同一个表的相同列进行比较。
--3、join关键字
select xsqk.姓名,xskc.课程名,xscj.成绩 from xscj
join xsqk on xsqk.学号=xscj.学号
join xskc on xskc.课程名=xscj.课程名
--if....else
if exists (select * from uds_proc where proc_id=1)
print '浏览结点权'
else
print '没有浏览结点权'
--无条件goto语句
declare @i int, @times int
select @i=1,@times=10
Label:
select @i=@i*@times
select @times=@times-1
if @times>=1
Goto Label
select @i,@times
--where
declare @i int,@sum int
select @i=0,@sum=0
while @i>=0
begin
select @i=@i+1
if @i>10
break
if (@i%2)=0
continue
else
select @sum=@sum+@i
end
select @i,@sum
--case<运算式> when<条件表达式> then<条件表达式>
select au_fname,au_lname,
case state
when 'CA' then 'then california'
when 'KS' then 'Kansas'
when 'TN' then 'Tennessee'
when 'OR' then 'Oregon'
when 'MI' then 'Michigan'
when 'IN' then 'Indiana'
when 'MD' then 'Maryland'
when 'UT' then 'Utah'
end as StateName
from pubs.dbo.authors
order by au_lname
--comupte by语句.使用COMPUTE BY子句对 BY后面给出的列进行分组显示,
--并计算该列的分组小计。使用时必须使用ORDER BY对BY指定的列进行排序。
select discounttype,lowqty,highqty,discount from pubs.dbo.discounts
order by discounttype
compute avg(discount),SUM(discount) by discounttype