how can i select 1,4,7,10,13,... numbered records from table [closed]
ID NAME 开发者_运维问答 AGE DEPTNO SALARY
1 shasank 25 11 2025
2 raju 27 12 2027
3 son 33 12 2131
6 bali 31 10 2031
4 don 33 11 2132
5 rambo 32 11 2121
7 dimpu 33 12 2314
8 chir 34 10 2123
9 nag 35 10 2213
10 ram 28 13 2141
Use the modulo operator? The mod()
form is the standard way, but a lot of db's use the %
operator too.
select * from table where MOD(ID,3) = 1;
SET @i = 0;
SELECT ..., ((@i := @i + 1) % 3) AS `i` FROM `table` ... HAVING `i` = 1
select * from table where id in (1,4,7,10,13)
SELECT * FROM table
WHERE ID % 3 = 1
Assuming, of course, that you are looking for every third ID.
Also, this is done with SQL Server - not sure what database you are using.
EDIT
Here is an option without using ID (SQL Server)
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY Name) RowNum
FROM table) X
WHERE X.RowNum % 3 = 1
精彩评论