sql查询

本文介绍了SQL的基础操作,包括列的选择、算术运算、删除重复行、排序、分组统计等功能,并展示了如何通过联合查询和连接查询来整合多个数据表。此外还提供了流程控制语句和条件表达式的使用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 --选择所有列
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值