how to show only even or odd rows in sql server 20

2020-01-31 01:00发布

i have a table MEN in sql server 2008 that contain 150 rows.

how i can show only the even or only the odd rows ?

thank's in advance

16条回答
We Are One
2楼-- · 2020-01-31 01:39

Try this :

odd :

select * from( 
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
FROM table1
) d where (RowNumber % 2) = 1 

even :

select * from( 
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
FROM table1
) d where (RowNumber % 2) = 0
查看更多
等我变得足够好
3楼-- · 2020-01-31 01:40
SELECT *
  FROM   
  ( 
     SELECT rownum rn, empno, ename
     FROM emp
  ) temp
  WHERE  MOD(temp.rn,2) = 1
查看更多
霸刀☆藐视天下
4楼-- · 2020-01-31 01:40
select * from Tablename 
where id%2=0
查看更多
Melony?
5楼-- · 2020-01-31 01:42

To select an odd id from a table:

select * from Table_Name where id%2=1;

To select an even id from a table:

select * from Table_Name where id%2=0;
查看更多
我想做一个坏孩纸
6楼-- · 2020-01-31 01:43

Following is for fetching even number:: Select * from MEN where Men_ID%2=0;

Following is for fetching odd number:: Select * from MEN where Men_ID%2!=0;

Here MEN is your table_name Men_ID is the column in MEN Table.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-01-31 01:45

FASTER: Bitwise instead of modulus.

select * from MEN where (id&1)=0;

Random question: Do you actually use uppercase table names? Usually uppercase is reserved for keywords. (By convention)

查看更多
登录 后发表回答