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 语句怎么写?
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 语句怎么写?
top 在 hql 语句中就不能通过,且我要的是Criteria 语句,谢谢大家, 求高手!!!
这东西能执行???
Criteria 是用setFirst... setMax... ,大哥,麻烦你看清楚点,我这条SQL 语句的表达的什么意思,谢谢!!
b是什么?in语句中表的别名,还是表的字段?
你这个语句是要查询什么?
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();
你就好比我要的是: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)
我没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的,所以没环境,你测试下,一切以测试结果为准。
呵呵,结果是不一样的,
比如:
用: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
根据你的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时的集合不是随机乱排的步就行了!
子语句中只有 top 3 你的查询结果竟然大于3!!! 怎么解释?
能开放你的数据结构和表数据不?
如果
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
字段:id int 主键
字段:b int