课程表的SQL语句

zitjubiz 2003-09-18 06:30:46
course表
classId 班级
subjectid 科目
timecode 时间代码(如A20) ABCDEFG表示星期1-7,2表示3-4节,0表示所有周
怎样写SQL语句可以排出课程表的样子
横行是星期1-7
竖行是节
...全文
122 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zitjubiz 2003-09-19
  • 打赏
  • 举报
回复
关于哪个课程表的SQL写法,0表示所有周,1为单周,2为双周
如果在星期三第3,4节课分单双周上不同的课,
你的SQL语句就只能找到一个课,应该怎样写才行呢?
请帮忙。
txlicenhe 2003-09-18
  • 打赏
  • 举报
回复
select classid 班级,substring(timecode,2,1) 课时,
max(case left(timecode,1) when 'A' then subjectid end) [星期1],
max(case left(timecode,1) when 'B' then subjectid end) [星期2],
max(case left(timecode,1) when 'C' then subjectid end) [星期3],
max(case left(timecode,1) when 'D' then subjectid end) [星期4],
max(case left(timecode,1) when 'E' then subjectid end) [星期5],
max(case left(timecode,1) when 'F' then subjectid end) [星期6],
max(case left(timecode,1) when 'G' then subjectid end) [星期7]
from course group by classid,substring(timecode,2,1)
zjcxc 元老 2003-09-18
  • 打赏
  • 举报
回复
你给的条件不足,一天一共有多少节?0表示所有周,如果不是0,则又怎么处理?

仅根据你上面给出的条件,假设每天有6节课,对应的值为1,2,3,则用下面的处理语句:

--每天有多少节课的信息
declare @tb table(id varchar(1),name varchar(10))
insert into @tb
select 1,'第一,二节'
union all select 2,'第三,四节'
union all select 2,'第五,六节'

--查询课程表
select classid 班级,课时=b.name,
max(case left(timecode,1) when 'A' then subjectid end) [星期1],
max(case left(timecode,1) when 'B' then subjectid end) [星期2],
max(case left(timecode,1) when 'C' then subjectid end) [星期3],
max(case left(timecode,1) when 'D' then subjectid end) [星期4],
max(case left(timecode,1) when 'E' then subjectid end) [星期5],
max(case left(timecode,1) when 'F' then subjectid end) [星期6],
max(case left(timecode,1) when 'G' then subjectid end) [星期7]
from course a right join @tb b on substring(a.timecode,2,1)=b.id
group by a.classid,substring(a.timecode,2,1)
pengdali 2003-09-18
  • 打赏
  • 举报
回复
create table #course(classId int,subjectid int,timecode varchar(100))
insert #course values(1,91,'A10')
insert #course values(1,92,'A20')
insert #course values(1,93,'A30')
insert #course values(1,94,'A40')
insert #course values(1,91,'B10')
insert #course values(1,92,'B20')
insert #course values(1,93,'B30')
insert #course values(1,94,'B40')
insert #course values(1,91,'C10')
insert #course values(1,92,'C20')
insert #course values(1,93,'C30')
insert #course values(1,94,'C40')
insert #course values(1,91,'D10')
insert #course values(1,92,'D20')
insert #course values(1,93,'D30')
insert #course values(1,94,'D40')
insert #course values(1,91,'E10')
insert #course values(1,92,'E20')
insert #course values(1,93,'E30')
insert #course values(1,94,'E40')
insert #course values(1,91,'F10')
insert #course values(1,92,'F20')
insert #course values(1,93,'F30')
insert #course values(1,94,'F40')
insert #course values(1,91,'G10')
insert #course values(1,92,'G20')
insert #course values(1,93,'G30')
insert #course values(1,94,'G40')

select classid 班级,substring(timecode,2,1) 课时,
max(case left(timecode,1) when 'A' then subjectid end) [星期1],
max(case left(timecode,1) when 'B' then subjectid end) [星期2],
max(case left(timecode,1) when 'C' then subjectid end) [星期3],
max(case left(timecode,1) when 'D' then subjectid end) [星期4],
max(case left(timecode,1) when 'E' then subjectid end) [星期5],
max(case left(timecode,1) when 'F' then subjectid end) [星期6],
max(case left(timecode,1) when 'G' then subjectid end) [星期7]
from #course group by classid,substring(timecode,2,1)
go
drop table #course
hjb111 2003-09-18
  • 打赏
  • 举报
回复
select classid 班级,subjectid 科目,
(case substring(timecode,1,1)
when 'a' then '星期1'
when 'b' then '星期2'
when 'c' then '星期3'
when 'd' then '星期4'
when 'e' then '星期5'
when 'f' then '星期6'
else '星期7' end) 星期,
(case substring(timecode,2,1)
when '1' then '第一节'
when '2' then '第二节'
else '第三节' end) 节次,
substring(timecode,3,1) 周次
from course
hjb111 2003-09-18
  • 打赏
  • 举报
回复
course表
classId 班级
subjectid 科目
timecode 时间代码(如A20) ABCDEFG表示星期1-7,2表示3-4节,0表示所有周
怎样写SQL语句可以排出课程表的样子
横行是星期1-7
竖行是节

select classid 班级,subjectid 科目,case(substring(timecode,1,1) when 'a' then '星期1' when 'b' then '星期2' when 'c' then '星期3' when 'd' then '星期4' when 'e' then '星期5' when 'f' then '星期6' else '星期7') 星期,case(substring(timecode,2,1) when '1' then '第一节' when '2' then '第二节' ) 节次,substring(timecode,3,1) 周次 from course
Rewiah 2003-09-18
  • 打赏
  • 举报
回复
select '1-2' as 节,max(case substring(timecode,1,1)
when 'A' then 科目 end) as 星期1,
max(case substring(timecode,1,2)
when 'B' then 科目 end) as 星期2,
...
from course
where classId=1
and timecode like '_1%'
union all
select '3-4' as 节,max(case substring(timecode,1,2)
when 'A' then 科目 end) as 星期1,
max(case substring(timecode,1,2)
when 'B' then 科目 end) as 星期2,
...
from course
where classId=1
and timecode like '_2%'
union all
....
pbtech 2003-09-18
  • 打赏
  • 举报
回复
严重关注,我做不出!
yun198183 2003-09-18
  • 打赏
  • 举报
回复
好像很复杂阿

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧