不好意思,这可以不是我的意思 改一下执行" from Table order by number,time desc "得到 ======================================================== id name number time 10 a 3 2007-11-26 00:00:50 5 b 2 2007-11-26 00:00:54 11 c 2 2007-11-26 00:00:30 3 d 2 2007-11-26 00:00:20 15 e 1 2007-11-26 00:00:18 ========================================================现在知道id为11,想得到id为10,5的二个记录。 也就是以字段order和字段time排序(desc),得到id为11以上的记录。
直接執行后,再取List里的前2條就OK 要么 select top 2 id,name,number,time from Table order by number,time desc
zs_han 表改成以下 执行" from Table order by number,time desc "得到 ======================================================== id name number time 10 a 3 2007-11-26 00:00:50 5 b 2 2007-11-26 00:00:54 11 c 2 2007-11-26 00:00:30 3 d 2 2007-11-26 00:00:20 15 e 1 2007-11-26 00:00:58 ======================================================== 我想得到 id以上的数据,(10,5)如果用你的得到的是(10,5,15)
那不是一样吗?过加一个条件,因为你是按number,time排序,直接加条件就可以了select count(*) from table where time > (select time from Table where id='11') and number > (select number from Table where id='11')
如果以下情况 ======================================================== id name number time 10 a 3 2007-11-26 00:00:05 5 b 2 2007-11-26 00:00:54 11 c 2 2007-11-26 00:00:30 3 d 2 2007-11-26 00:00:20 15 e 1 2007-11-26 00:00:58 ========================================================那你只得到Id是5的记录, 得不到5和10
我F了U,具体问题具体看一下,加个等号不就可以了吗,你自己不能调试一下吗?select count(*) from table where time > (select time from Table where id='11') and number >= (select number from Table where id='11')
你在查询分析器执行一下试试。这个就是你要的结果 select count(*) from table where time> (select time from table where id='11') or number> (select number from table where id='11')
搞定了,原来还是我搞复杂了select count(*) from table where (time > (select time from table where id='11') and number = (select number from table where id='11')) or number> (select number from table where id='11')
from Table t where t.id<3 order by t.order desc,t.time desc
Table就是你這個表對應的類名
id,order,time就是這個類相應的屬性名
都大小寫敏感的
改一下执行" from Table order by number,time desc "得到
========================================================
id name number time
10 a 3 2007-11-26 00:00:50
5 b 2 2007-11-26 00:00:54
11 c 2 2007-11-26 00:00:30
3 d 2 2007-11-26 00:00:20
15 e 1 2007-11-26 00:00:18
========================================================现在知道id为11,想得到id为10,5的二个记录。 也就是以字段order和字段time排序(desc),得到id为11以上的记录。
要么
select top 2 id,name,number,time from Table order by number,time desc
我就是这个意思
===================================================================================
靠rownum來做? 不然如果db不支援rownum, 依照你需求
是要(number > id為11的number ) || (number = id為11的number && time >= id為11的time )
這樣?
====================================================================================
http://www.javaworld.com.tw/jute/post/view?bid=54&id=212424&sty=1谢谢高手能继续关注
就是當從數據庫里每取出一條記錄 就會自動給這條記錄加上一個row num(字面意思就是行號)標示你得到id=11的記錄 你就可以同時得到id=11的那條記錄的rownum
然后再取rownum > id為11的number 即可
如果不明白就是查一下rownum的用法吧昨天本來想跟你說用這種方式的,但仔細看你的提問,好像沒什么必要將這些,就沒將了。
你說要hql寫,好像不支持rownum吧。所以呢 ,還是直接取出記錄來,在操縱結果集吧。
如果id是主建时就不需要 and rownum<2东西了
要SQL的話去SQL版塊問吧。
感覺有點郁悶。剛開始說:
知道id为3,想得到id为1,2的二个记录。
后又說:
我是想知道id为11以上的数据有多少条
又說:
number > id为11的number
执行" from Table order by number,time desc "得到
========================================================
id name number time
10 a 3 2007-11-26 00:00:50
5 b 2 2007-11-26 00:00:54
11 c 2 2007-11-26 00:00:30
3 d 2 2007-11-26 00:00:20
15 e 1 2007-11-26 00:00:58
========================================================
我想得到
id以上的数据,(10,5)如果用你的得到的是(10,5,15)
========================================================
id name number time
10 a 3 2007-11-26 00:00:05
5 b 2 2007-11-26 00:00:54
11 c 2 2007-11-26 00:00:30
3 d 2 2007-11-26 00:00:20
15 e 1 2007-11-26 00:00:58
========================================================那你只得到Id是5的记录,
得不到5和10
这的结果还只能是1,得不到2,因为id为10的记录时间不大于id为11的时间下面的网站的楼主的意思就是我要的
http://www.javaworld.com.tw/jute/post/view?bid=54&id=212424&sty=1
select rownum as a,id
from "COSCOOA"."TBL_TABLE" order by number_,time) where id=11
可是我用的是mysql,我还想知道在hiberate下有什么好方法不知怎么解决,谢谢shaoml你的方法可能还是不行
select count(*) from table where time> (select time from table where id='11') or number> (select number from table where id='11')