日期:2014-05-17  浏览次数:20367 次

jsp中My SQL 嵌套查询极端难题
紧急求助…最近在做以网页,实现一个网页考勤系统,用jsp读mysql写的,有两张表,student和state,用student.std_mac和state.student_id关联起来的,state表中存储的数据量比较大,而且有重复,因为每个人每天都会添加一条记录,而student表中的记录是固定的,state中有一个字段标志位(名字也叫state),每天只要有人上线,就会给他加一条记录,并且把标志位置1,下线之后变为0,如果既不上线也不下线的话,数据库中不会增加记录,state中还有一个字段login_date,用来记录某人最近一次上线的日期时间。现在我的问题是,想读取出此时此刻不在线的人员信息,包括今天上线之后又下线的人和今天既没有上线也没有下线的人(因为今天既没上线也没下线的人在数据库里没有记录,所以就读取他们最近一次上线的日期),在jsp里面我用字符串sql来存储select语句,然后读取数据库显示在网页。请问各位大神,我该怎么用一句话就读取出所有此时此刻不在线的人员信息呢,跪求……
下面是我之前尝试写的select语句,但是把当前在线人员之前的离线信息也读出来了,求大神帮忙解决:
select std_name,a_login_time,a_logout_time,p_login_time,p_logout_time,n_login_time,n_logout_time,online_t,student_id,login_date from 
(select a_login_time,a_logout_time,p_login_time,p_logout_time,n_login_time,n_logout_time,online_t,student_id,login_date 
from
(select a_login_time,a_logout_time,p_login_time,p_logout_time,n_login_time,n_logout_time,online_t,student_id,login_date from state 
where state=0 order by login_date desc ) 
as a group by student_id) 
as c left join student 
as b on c.student_id=b.std_mac
注:
a_login_time,a_logout_time,p_login_time,p_logout_time,n_login_time,n_logout_time,online_t,student_id,login_date都是state表里面的字段
std_mac,std_nam是student里面的字段


------解决方案--------------------
探讨

select *
from student
where std_mac not in (select student_id from state where state=1)

------解决方案--------------------
select a.std_name,max(c.login_date) as 最后上线时间
from student as a left join state as c on a.std_mac=c.student_id
where not exists(select 1 from state as b where a.std_mac=b.student_id and b.state=1)
group by a.std_name