1、创建一个表,表结构如下:
学号char (10),姓名 varchar(8),入学日期 char(10) --备注:格式为yyyy-mm-dd
问题:如何使得 Char类型的 入学日期 格式约束为 YYYY-MM-DD ???
2、写一存储过程
把成绩表中字段 chinese ,math 求和,并把和值写入到总分字段中;
按如下规则:先总分排名,若总分一样 则按语文成绩排名,如语文成绩一样,则名次相同,把名次写入名次字段中。
如:姓名 语文 数学 总分 名次
张三 98 90 ? ?
李四 100 88 ? ?
请问:该如何写这个存储过程?
十分感激!
------解决方案--------------------
here you go:
- SQL code
-- step 1create Table #t (studentID char(10), [name] varchar(8), startDate char(10) Check(substring(startDate,5,1)='-' AND substring(startDate,8,1)='-' AND isdate(startDate)=1))INSERT #t values('1','a','012956')INSERT #t values('112996')insert #t values('1','a','2001-01-04')SELECT * FROM #tdrop table #t-- step 2create table #tc ([name] varchar(8), chinese float, math float)insert #tcselect 'a', 80, 90union all select 'b', 85, 88union all select 'c', 77, 93union all select 'd', 80, 90union all select 'e', 99, 100create procedure showRankasbegin select [name], chinese, math, chinese+math as total, rank() over (order by chinese+math, chinese) as rank from #tc order by chinese+mathendexec showRankdrop table #tc
------解决方案--------------------
1、创建一个表,表结构如下:
学号char (10),姓名 varchar(8),入学日期 char(10) --备注:格式为yyyy-mm-dd
问题:如何使得 Char类型的 入学日期 格式约束为 YYYY-MM-DD ???
create table tb(学号 char(10),姓名 varchar(8),入学日期 datetime)
2、写一存储过程
把成绩表中字段 chinese ,math 求和,并把和值写入到总分字段中;
按如下规则:先总分排名,若总分一样 则按语文成绩排名,如语文成绩一样,则名次相同,把名次写入名次字段中。
如:姓名 语文 数学 总分 名次
张三 98 90 ? ?
李四 100 88 ? ?
SELECT * , 名次=(SELECT COUNT(总分) FROM
(
select 姓名,语文,数学,总分 = 语文 + 数学 from tb
) t
WHERE 总分 > a.总分 or (总分 = a.总分 and 语文 > a.语文)) + 1
FROM
(
select 姓名,语文,数学,总分 = 语文 + 数学 from tb
) a
ORDER BY 名次
------解决方案--------------------
第2个问题:
create table #tc ([name] varchar(8), chinese float, math float)
insert #tc
select 'a', 80, 90
union all select 'b', 85, 88
union all select 'c', 77, 93
union all select 'd', 80, 90
union all select 'e', 99, 100
select * from #tc
select *,total=chinese + math,rank=(select count(1)+1 from #tc b where b.chinese + b.math>a.chinese + a.math ) from #tc a order by rank
drop table #tc
------解决方案--------------------
- SQL code
--对输入的数据进行约束create table t(studentID char(10), [name] varchar(8), startDate char(10) Check (isdate(startdate)=1 and cast(startdate as datetime)=convert(datetime,startdate,120)))insert into tselect 'aa','bb','77799820'union all select 'bb','ggg','2007-10-11'union all select 'cc','ddd','2007-20-11'select * from t--创建测试表create table tbl ([name] varchar(8), chinese float, math float,zong float,px int)insert tblselect 'a', 80, 90,null,nullunion all select 'b', 85, 88,null,nullunion all select 'c', 77, 93,null,nullunion all select 'd', 80, 90,null,nullunion all select 'e', 99, 100,null,null--创建存储过程create proc mysql asupdate bb set zong=tt.zong,px=tt.px from tbl bb,(SELECT name,zong , px=(SELECT COUNT(zong) FROM ( select name,chinese,math,zong = chinese + math from tbl ) t WHERE zong > a.zong or (zong = a.zong and chinese > a.chinese)) + 1 FROM ( select name,chinese,math,zong = chinese + math from tbl ) a ) ttwhere bb.name=tt.namego--调用存储过程exec mysql--查看select * from tbl--删除drop table tbldrop proc mysql
------解决方案--------------------
收下了