是一个教室使用方面的冲突检测的SQL语句
首先是表一:
教室表编号 教室 最大使用班级数(也就是这个教室在一天只能允许几个班级使用)
001 中文 2教室使用情况表
教室编号 班级 开始日期 结束日期
001 A 2006-03-01 2006-03-28
001 B 2006-03-01 2006-03-06
001 c 2006-03-07 2006-03-28上面的内容说明了,在2006-03-01到2006-03-28之间有三个班,但每天保证都是在一个教室里不超过两个班上课(教室表中限定),
我现在,想传入条件,教室编号,开始日期,与结束日期,三个条件,检测教室使用是否冲突(即超过教室表里的最大班级数的约定),想了一天也没有好的方法,因为是时间范围与时间范围的比较,希望高手给解。特别注明,此乃本世纪最难SQL语句。希望大家用存储过程做,如果没有冲突返回0,有冲突返回1,
首先是表一:
教室表编号 教室 最大使用班级数(也就是这个教室在一天只能允许几个班级使用)
001 中文 2教室使用情况表
教室编号 班级 开始日期 结束日期
001 A 2006-03-01 2006-03-28
001 B 2006-03-01 2006-03-06
001 c 2006-03-07 2006-03-28上面的内容说明了,在2006-03-01到2006-03-28之间有三个班,但每天保证都是在一个教室里不超过两个班上课(教室表中限定),
我现在,想传入条件,教室编号,开始日期,与结束日期,三个条件,检测教室使用是否冲突(即超过教室表里的最大班级数的约定),想了一天也没有好的方法,因为是时间范围与时间范围的比较,希望高手给解。特别注明,此乃本世纪最难SQL语句。希望大家用存储过程做,如果没有冲突返回0,有冲突返回1,
解决方案 »
- 创建用户问题
- 求sql?
- 这个存储过程怎么写?
- 新建的用户,局域网内登陆不了,只有本机才能登陆。
- 通过链接服务器查询Mysql出错的问题!
- 小弟做一个木制厂的系统,不知数据库怎么设计,好像应该用DOM
- VS2010中自带的sql sever怎么可以直接使用
- 紧急问题!!!!如何通过sql查询到某个表的哪些字段是索引,哪些字段是唯一,那个字段是主键???????????
- 光标的问题
- ((@)(@))ODBC-DBASE 5.0 数据库,单用户,在删除某行记录时,发生警告"由于表被占用,无法锁定数据库",查询代码在此之前并无其他连接(op
- 重新排序问题
- 请SQL SERVER 版的兄弟们帮帮忙,在Postgre中如何使用删除的触发器,谢谢!
declare @numall int,@num int
create table #class (classid char(10),name char(20),num int )
insert into #class
select '001','中文',2 union all
select '002','数学',2 union all
select '003','英语',3 create table #classuser (classid char(10),banji char(20),begindate char(10),enddate char(10))
insert into #classuser
select '001','A','2006-03-01','2006-03-28' union all
select '001','B','2006-03-01','2006-03-06'
--select '001','C','2006-03-07','2006-03-28' select @numall=count(*)
from #class
where classid=@classid select @num=count(*)
from #classuser
where classid=@classid
and begindate>=@begindate
and enddate>=@enddate
if @numall>=@num
print '0' if @numall<@num
print '1'
GO
CREATE PROCEDURE testqw @classid char(10), @begindate char(10), @enddate char(10) as
declare @numall int,@num int
create table #class (classid char(10),name char(20),num int )
insert into #class
select '001','中文',2 union all
select '002','数学',2 union all
select '003','英语',3 create table #classuser (classid char(10),banji char(20),begindate char(10),enddate char(10))
insert into #classuser
select '001','A','2006-03-01','2006-03-28' union all
select '001','B','2006-03-01','2006-03-06'
--select '001','C','2006-03-07','2006-03-28' select @numall=count(*)
from #class
where classid=@classid select @num=count(*)
from #classuser
where classid=@classid
and begindate>=@begindate
and enddate>=@enddate
if @numall>@num
print '0' if @numall<=@num
print '1'
GO
where
(
select count(*) from 教室使用情况表 t2
where t2.教室编号=t1.教室编号
and
(
(@Begindate>=t2.开始日期 and @Enddate>=t2.结束日期)
or (@Begindate>=t2.开始日期 and @Enddate>=t2.结束日期)
)
)>2上面语句可以查出你指定日期范围内的所有教室如果要查指定教室的只要加个条件就可以了