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条回答
冷血范
2楼-- · 2020-01-31 01:55

Check out ROW_NUMBER()

SELECT t.First, t.Last
FROM (
    SELECT *, Row_Number() OVER(ORDER BY First, Last) AS RowNumber 
            --Row_Number() starts with 1
    FROM Table1
) t
WHERE t.RowNumber % 2 = 0 --Even
--WHERE t.RowNumber % 2 = 1 --Odd
查看更多
一夜七次
3楼-- · 2020-01-31 02:03

Assuming your table has auto-numbered field "RowID" and you want to select only records where RowID is even or odd.

To show odd:

Select * from MEN where (RowID % 2) = 1

To show even:

Select * from MEN where (RowID % 2) = 0
查看更多
放荡不羁爱自由
4楼-- · 2020-01-31 02:05

For even values record :

select * from www where mod(salary,2)=0;

For odd values record:

select * from www where mod(salary,2)!=0;
查看更多
放我归山
5楼-- · 2020-01-31 02:05

Try following

SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
查看更多
登录 后发表回答