sql排序、oracle的rownum相关。

10:15sql


数据库中,一个agentcode可能对应着多个enddate,这儿须要取出排序后最顶上一个。数据库

个人想法是将全部enddate用code

select enddate from t_agreement where agentcode = "00000001";

来取出来存放到一个List中,而后试用String的replace方法将其中的"/"字符去掉。再用快排将其进行排序,以后取第一个,加上"/"。完成。排序


龙哥的说法是,你直接在sql取的时候就排好不就行了啊。。。table

select max(enddate) from t_agreement where agentcode = "000000001";


就这样。class

仍是对sql理解不够深入。。date


14:50List


t_orclselect

id type
11 a
3 b
2 c
6 d
5 e
8 f

上述表中按 id 排序后取出第三行元素。
方法


首先咱们要作的是按 id 排序。

select * from t_orcl order by id;


接下来,为了保证rownum顺序是正确的,咱们只能嵌套一层select:

select id, type, rownum rn from (select * from t_orcl order by id);


这样咱们获得了这样一张表:

id type rn
2 c 1
3 b 2
5 e 3
6 d 4
8 f 5
11 a 6


而后呢咱们就能够取第三行了:

select * from (select id, type, rownum rn from (select * from t_orcl order by id)) t where t.rn = 3;


id type rn
5 e 3


就是这样,简单明了。

固然了,还有另一种方法,呐,或许好用。

select * from (select id, type, rownum from (select * from t_orcl order by id) where rownum < 4) having id = max(id);


对了当你用这种逗比语句受到嘲讽的时候请不要甩锅给我谢谢。

相关文章
相关标签/搜索