sql语句:select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)查询结果:
id          b           
----------- ----------- 
241349      150020
241350      150020
241351      150020
243710      500038
243711      500038
243712      500038
258461      110003
258462      110003
258463      110003
242645      500005
242646      500005
242647      500005求Criteria 语句怎么写?

解决方案 »

  1.   

    from 实体类名称 a where a.id in (select top 3 id from 实体类名称 where b = a.b)
      

  2.   

    from 映射实体类名(注:Hibernate初始化阶段根据映射元数据推导出来的) a where a.id in (select top 3 id from 映射实体类名 where b = a.b)
      

  3.   

    ...
      top  在 hql 语句中就不能通过,且我要的是Criteria 语句,谢谢大家, 求高手!!!
      

  4.   

    Criteria 是用setFirst... setMax... 来实现的  mysql用的是limit  当然在hql中不能用top 了   Croteria 里有in  方法的 select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    这东西能执行???
      

  5.   

    from 实体类名称 a where a.id in (select top 3 id from 实体类名称 where b = a.b)
      

  6.   

    sql server 里能执行 select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    Criteria 是用setFirst... setMax...  ,大哥,麻烦你看清楚点,我这条SQL 语句的表达的什么意思,谢谢!!
      

  7.   


    b是什么?in语句中表的别名,还是表的字段? 
    你这个语句是要查询什么?
      

  8.   

    谢谢,你说很简单,麻烦你怎么解决那个 top 的问题,谢谢!!
      

  9.   


    SQL Server 的语法:SELECT TOP number|percent column_name(s)
    FROM table_nameMySQL 和 Oracle 中的 SQL SELECT TOP 是等价的
    MySQL 语法SELECT column_name(s)
    FROM table_name
    LIMIT number例子SELECT *
    FROM Persons
    LIMIT 5Oracle 语法SELECT column_name(s)
    FROM table_name
    WHERE ROWNUM <= number例子SELECT *
    FROM Persons
    WHERE ROWNUM <= 5
    那么你的
    select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    在mysql中等价于:
    select * from tb1 a where a.id (select c.id from tb1 c where a.b = c.b) limit 3
    在oracle中等价于:
    select * from (select * from tb1 a where a.id (select c.id from tb1 c where a.b = c.b) ) where rownum <=3由于存在子查询,那么使用Criteria 接口,其实是无法全部对象化的。Criteria crit = session.createCriteria(TB1.class);//可以使用createCriteria(TB1.class,"别名"),不过我在本地测试了下,貌似没作用,默认的还是this_ 你可以在自己的环境下测试。这里的TB1是javabean名字
    crit.add(Restrictions.sqlRestriction("id in (select id from tb1 c where b = c.b)"));//这里的tb1是物理表名
    crit.setMaxResults(3);
    List<TB1> list = crit.list();
      

  10.   

    这位大哥,crit.setMaxResults(3);你用的位置不对啊,不符合我查询的结果啊
    你就好比我要的是:select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    你改成了这样:select top 3 * from tb1 a where a.id in (select id from tb1 where b = a.b)
      

  11.   


    我没SQLserver环境,你自己测试下这两句的结果一样不。
    select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    select top 3 * from tb1 a where a.id in (select id from tb1 where b = a.b)
    两者我认为是一样的。因为在MYSQL5中 根据文档 top 相当于 mysql 中的 limit 语句。但在mysql5中,子查询语句中是不能出现limit关键字的。所以SQLserver你自己跑下上述语句了。看结果一样不。我这一般很少用hibernate连接SQLserver的,所以没环境,你测试下,一切以测试结果为准。
      

  12.   


    呵呵,结果是不一样的,
    比如:
    用:select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    查询结果:
    id b   
    ----------- -----------  
    241349 150020
    241350 150020
    241351 150020
    243710 500038
    243711 500038
    243712 500038
    258461 110003
    258462 110003
    258463 110003
    242645 500005
    242646 500005
    242647 500005用:select top 3 * from tb1 a where a.id in (select id from tb1 where b = a.b)
    查询结果:
    id b   
    ----------- -----------  
    241349 150020
    241350 150020
    241351 150020
      

  13.   


    根据你的SQL
    如果子查询符合条件的id = {1,3,5,7,8}  top 3  =>   {1,3,5}
    如果所有 all_id = {1,2,3,4,5,6,7,8,9,10....} 那么select * from tb1 a where a.id in (select top 3 id from tb1 where b = a.b)
    是 先拿到 {1,3,5} 接着 all_id in {1,3,5)}  结果是 {1,3,5}
    而select top 3 * from tb1 a where a.id in (select id from tb1 where b = a.b)
    子查询是 id = {1,3,5,7,8}
    主语句是 all_id = {1,2,3,4,5,6,7,8,9,10....}  这两个的交集是id = {1,3,5,7,8} 在 top3 结果是{1,3,5}理论上是一样的。有区别吗? 只要保证 top 集合时的规则相同不就行了,把top用在里面和用在外面数据库生成的集合规则不同你认为可能么?如果不是推断出来的这样,那你对id排序了,确保每次执行该语句时,top时的集合不是随机乱排的步就行了!
      

  14.   


    子语句中只有 top 3  你的查询结果竟然大于3!!! 怎么解释?
    能开放你的数据结构和表数据不?
      

  15.   

    呵呵,我解释下:
    如果 
    b = {1,2,3}
    id={1001,1002,1003,1004,2001,2002,2003,2004,3001,3002,3003,3004}我查询的结果是为:list = {1001,1002,1003,2001,2002,2003,3001,3002,3003}
    不是为:list = {1001,1002,1003}
    你要注意子查询里的查询条件where b = a.b
      

  16.   

    相同b值的,只取前面3条记录,多个b值,就不只是3条记录了,你注意没有,每3条记录b值都是不样的
      

  17.   

    表:tb1
    字段:id int 主键
    字段:b int
      

  18.   

    唉,现在csdn怎么就没有高手啊,一个这样的小问题都没有人解答出来。