sql server 学习小记1

--====================
--author:yeeXun
--date:2010-11-21
--address:17-304
--====================

use practiceDB
go
create table 成绩表 (姓名 varchar(4),科目 varchar(4),分数 int);
insert into 成绩表
select '张三','数学',90 union all
select '李四','数学',80 union all
select '王五','数学',70 union all
select '张三','语文',60 union all
select '李四','语文',50 union all
select '王五','语文',40 union all
select '赵六','数学',30 union all
select '赵六','语文',20
go

create table 补考成绩表(姓名 varchar(4),科目 varchar(4),分数 int);
insert into 补考成绩表
select '李四','语文',70 union all
select '王五','语文',75
go
/*
require:
1、想把成绩表中不及格的,并参加补考及格的,更新成“补及”。
2、想把成绩表中不及格的,并参加补考及格的,更新成补考成绩。
*/

alter table 成绩表 alter column 分数 varchar(10)
select * from 补考成绩表
select * from 成绩表
go
update 成绩表
set 分数='补及'
from 成绩表 a,补考成绩表 b
where a.姓名=b.姓名 and a.科目=b.科目 and cast(b.分数 as int)>60;

go
select * from 成绩表
go
drop table 成绩表
drop table 补考成绩表


别人的解法
declare @成绩表 table (姓名 varchar(4),科目 varchar(4),分数 int)
insert into @成绩表
select '张三','数学',90 union all
select '李四','数学',80 union all
select '王五','数学',70 union all
select '张三','语文',60 union all
select '李四','语文',50 union all
select '王五','语文',40 union all
select '赵六','数学',30 union all
select '赵六','语文',20

declare @补考成绩表 table (姓名 varchar(4),科目 varchar(4),分数 int)
insert into @补考成绩表
select '李四','语文',70 union all
select '王五','语文',75

select a.*,b.科目 as 补及,b.分数 as 新分数 from @成绩表 a
left join @补考成绩表 b
on a.姓名=b.姓名
and a.科目=b.科目


========================================================
create table #tbA(fcode char(7),incode char(3),qname char(3))
create table #tbB(fcode char(7),name char(4))
go
insert into [#tbA]
select 'f123456','000','000' union all
select 'f234567','111','222'
go
insert into [#tbB]
select 'f123456','张三' union all
select 'f234567','李四'
go
/*
require:
当 incode=qname 输出 fcode qname
当 incode<>qname 输出 fcode name
*/
solution 1:
select a.fcode,a.qname as mingzi
from [#tbA] a, [#tbB] b where a.fcode=b.fcode and a.incode=a.qname
union all
select a.fcode,b.name as mingzi
from [#tbA] a, [#tbB] b where a.fcode=b.fcode and a.incode<>a.qname
go
solution 2:
select a.fcode,
case when a.incode=a.qname then a.qname else b.name end as mingzi
from [#tbA] a inner join [#tbB] b on a.fcode=b.fcode

drop table [#tbB]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值