SQL查询备用

DATE: 2016-11-10 / VIEWS: 503

//每个分类获取一条记录
Select id,exp_name,exp_anclass,adddate FROM cq_products AS T Where ID IN(Select TOP 1 ID FROM cq_products Where exp_anclass=T.exp_anclass and site_language = '"&site_language&"' orDER BY id DESC)

//Mysql 联表查询
update tp_forum_topics a, tp_app_childmodule b set a.app_id= b.app_id where a.childmodule_id=b.id;

//最近记录
select id,exp_name from cq_products where datediff('d','"&now()&"',exp_start) >= 0 order by exp_start asc,id desc

//酒店订房会用到,备用
设计一个表:房间预占状态表 A
字段名:fjh (房间号),rq日期rq,zt(占用状态,0未占用,1已预占)
这个表保存每个房间,每天(比如将来的6个月)的预订情况。
select fjh,count(*) "空闲天数" from A where rq between dt1 and dt2 and zt=0
group by fjh having count(*)=dt2-dt1;
这个语句可以查出哪些房间在这个时间范围内空闲,且count(*)空闲天数,要等dt1与dt2之间的天数。
当然了一个房间的空闲天数可能还不止这个天数,但用rq between dt1 and dt2 已经做了限制。

有两个表,或一个表都可以,从设计上来说如果用一个表来记录的所有非free状态(或含三种状态)的日期,在使用时会方便一些。我的建议是一个表记录房间的非free日期的记录,即把入住信息与预订信息放一起,如果是分开的两个表,也可以做个合集。还有一个房间的基本信息,里面有没有状态都无所谓,看你怎么设计,决定在使用时的查询方式不一样。
结果可以得到busy和booked状态的房间合集。然后找房间表A中的房间号且日期不在占用记录中,即是可预订的房间。
select * from A
   where zt='free'
     or zt<>'free' and fjh not in (select distinct fjh from B where b.rq between dt1 and dt2 )
说明:如果zt='free' 则直接可以给客户预订
如果zt<>'free' ,则房间号不能是在指定日期dt1与dt2之间已经busy或booked的。
其中B表是被busy和booked的合集,你可以用一查询来代替。