表结构
Table_Name test
id 自动增长 主键     --学号
name 字符            --姓名
class  int           --班级
date  datetime       --入学时间现要求需要取得 每个班级中前N个入学的同学姓名、学号、班级、入学时间等信息。
目前在其他数据库中已经实现
select * from test  where  test.id in (select top N id from test as t where test.class=t.class order by t.class,t.date asc) order by test.class,test.date但在MYSQL中不支持TOP关键字  用LIMIT又无法实现 请教如何解决。。谢谢 
select * from test  where  test.id in (select  id from test as t where test.class=t.class order by t.class,t.date asc limit N) order by test.class,test.date

解决方案 »

  1.   

    SQL SERVER :
    select top 8 * from table1
    go
    MYSQL:
    select * from table1 limit 5;
      

  2.   

    不知道你使用的mysql版本是什么?mysql4是不支持子查询的
      

  3.   

    mysql 5.0.41也不支持子查询和limit一起使用的,楼主的问题大概只能用多个查询来实现.
      

  4.   

    根本就用不到子查询啊,select * from test where class='x' order by id limit n ;这样不行吗?
      

  5.   

    5.0以上版本limit 不能放在子查询中
    你这语句不需要子查询
      

  6.   

    select *
    from test t
    where N>(select count(*) from test where class=t.class and `date`<t.date)