student表中有字段,classid,studentid,value,time四个字段
classid为班级号,studentid为学号,value为成绩,time为时间
例如:
classid studentid value time
1 12 98 2009-08-01
2 2 78 2009-08-01
1 12 66 2009-08-05
3 6 85 2009-08-06
2 2 69 2009-08-23
1 2 45 2009-08-04现在要查询所有学生最新的一条记录,sql语句怎么写?也就是查询最终结果是:
classid studentid value time
1 12 66 2009-08-05
3 6 85 2009-08-06
2 2 69 2009-08-23
1 2 45 2009-08-04
classid为班级号,studentid为学号,value为成绩,time为时间
例如:
classid studentid value time
1 12 98 2009-08-01
2 2 78 2009-08-01
1 12 66 2009-08-05
3 6 85 2009-08-06
2 2 69 2009-08-23
1 2 45 2009-08-04现在要查询所有学生最新的一条记录,sql语句怎么写?也就是查询最终结果是:
classid studentid value time
1 12 66 2009-08-05
3 6 85 2009-08-06
2 2 69 2009-08-23
1 2 45 2009-08-04
解决方案 »
- 关于Information_schema和Mysql库的问题.
- 关于服务器系统更新重启后PostgreSQL8.1服务无法启动
- 有关日文乱码问题:Mysql中数据是日文、取下来就变成???了
- mysql server安装完以后,无法启动服务?
- 大家觉得把我原来用的MySQL4.0.18升级成MySQL5.0好不?
- mysql如何实现'条件 like 字段%'
- SOS,急等。如何解除表文件8M的限制!!
- phpMyAdmin2.3.0的问题,求教!
- 请问这句什么意思mysql>flush privileges;
- 请问我如何WIN2000下装mysql的jdbc驱动,以及如何连接
- mysql 大数据量问题
- mysql查询sql语句
left join tt b on a.studentid=b.studentid and a.time<=b.time
group by a.classid,a.studentid,a.value,a.time
having count(b.time)=1
where not exist (select 1 from student where studentid=s.studentid and time>s.time);
time in
(select time from tt where studentid=a.studentid order by time desc limie 1)
where not exist (select 1 from student where studentid=s.studentid and classid=s.classid and time>s.time);或者select s.*
from student s inner join (select classid,studentid,max(time) as mtime from student group by classid,studentid) b
on s.classid=b.classid and s.studentid=b.studentid and s.time=b.mtime
mysql> select * from student;
+---------+-----------+-------+------------+
| classid | studentid | value | time |
+---------+-----------+-------+------------+
| 1 | 12 | 98 | 2009-08-01 |
| 2 | 2 | 78 | 2009-08-01 |
| 1 | 12 | 66 | 2009-08-05 |
| 3 | 6 | 85 | 2009-08-06 |
| 2 | 2 | 69 | 2009-08-23 |
| 1 | 2 | 45 | 2009-08-04 |
+---------+-----------+-------+------------+
6 rows in set (0.00 sec)
mysql> select t1.* from student t1 where not exists
-> (select 1 from student where
studentid=t1.studentid and classid=t1.classid and time>t1.time);
+---------+-----------+-------+------------+
| classid | studentid | value | time |
+---------+-----------+-------+------------+
| 1 | 12 | 66 | 2009-08-05 |
| 3 | 6 | 85 | 2009-08-06 |
| 2 | 2 | 69 | 2009-08-23 |
| 1 | 2 | 45 | 2009-08-04 |
+---------+-----------+-------+------------+
4 rows in set (0.00 sec)
`time`=
(SELECT `time` FROM tgh WHERE studentid=a.studentid AND classid=a.classid ORDER BY `time` DESC LIMIT 1)orSELECT a.classid,a.studentid,a.value,a.time FROM tgh a
LEFT JOIN tgh b ON a.studentid=b.studentid AND a.classid=b.classid AND a.time <=b.time
GROUP BY a.classid,a.studentid,a.value,a.time
HAVING COUNT(b.time)=1
select 1 from student where studentid=s.studentid and classid=s.classid and time>s.time
这个好像mysql不支持select 1语法
为什么不试一下?mysql> select 1 from a;
+---+
| 1 |
+---+
| 1 |
| 1 |
+---+
2 rows in set (0.00 sec)mysql>
是选出数字1,不是第一列
classid为班级号,studentid为学号,value为成绩,time为时间
例如:
classid studentid value time
1 12 98 2009-08-01
2 2 78 2009-08-01
1 12 66 2009-08-05
3 6 85 2009-08-06
2 2 69 2009-08-23
1 2 45 2009-08-04
--------------------------
select classid,studentid,value,max(time) from student group by classid,studentid
from student,
(select classid,studentid,max(time) m
from student
group by classid,studentid) a
where student.classid=a.classid
and student.studentid=a.studentid
and student.time=a.m
这个是正确的
from student,
(select classid,studentid,max(time) m
from student
group by classid,studentid) a
where student.classid=a.classid
and student.studentid=a.studentid
and student.time=a.m
这个是正确的
studentid、classid建立索引没有
这个需要看一下你的表中的索引了。要么根据你目前索引的情况来修改SQL语句,要么根据常用的SQL语句来设置索引。