开发者

how can i select 1,4,7,10,13,... numbered records from table [closed]

开发者 https://www.devze.com 2023-03-21 23:40 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
 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
0

精彩评论

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