开发者

how to fetch 1 by 1

sno acco_no amount

1    50001     5000
2    50002     4000
3    50001     2500
4    50002     3100
5    50002     3400
6    50001     1500

I want to take 50001's last 2 records one by one.

 select sno, acco_no, amount 
 from table 
 where acco_no = 50001 
 order by tno desc fetch first 2 rows only

sno acco_no amount
  6     50001     1500
  3    50001      2500

but I want to fetch 1 by 1 record like following

1) first step

select sno, acco_no开发者_如何学C, amount 
from table 
where acco_no = 50001 

sno acco_no amount
  6     50001     1500

2) second step

select sno, acco_no, amount 
from table 
where acco_no = 50001

sno acco_no amount
  3     50001     2500

Note : should not delete any records


you can simply use LIMIT / OFFSET for that stuff, depends on the database you use... for Postgres: http://www.postgresql.org/docs/8.1/static/queries-limit.html

for MySQL the Limit-Keyword have 2 parameters for limit and offset-definition

1st

select sno, acco_no, amount 
from table 
where acco_no = 50001 LIMIT 0,1

sno acco_no amount
  3     50001     2500

2nd

select sno, acco_no, amount 
from table 
where acco_no = 50001 LIMIT 1,1

sno acco_no amount
  3     50001     2500
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜