SQL 查找偶数或奇数 ID 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8100622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding even or odd ID values
提问by Phoenix
I was working on a query today which required me to use the following to find all odd number ID values
我今天正在处理一个查询,它要求我使用以下内容来查找所有奇数 ID 值
(ID % 2) <> 0
Can anyone tell me what this is doing? It worked, which is great, but I'd like to know why.
谁能告诉我这是在做什么?它有效,这很棒,但我想知道为什么。
回答by Dair
ID % 2
is checking what the remainder is if you divide ID by 2. If you divide an even number by 2 it will always have a remainder of 0. Any other number (odd) will result in a non-zero value. Which is what is checking for.
ID % 2
正在检查如果将 ID 除以 2 的余数是多少。如果将偶数除以 2,则余数始终为 0。任何其他数字(奇数)将导致非零值。这就是检查的内容。
回答by anandharshan
For finding the even number we should use
为了找到我们应该使用的偶数
select num from table where ( num % 2 ) = 0
回答by Menuka Ishan
As Below Doc specify
如下文档指定
dividend % divisor
Returns the remainder of one number divided by another.
股息 % 除数
返回一个数除以另一个数的余数。
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/modulo-transact-sql#syntax
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/modulo-transact-sql#syntax
For Example
例如
13 % 2 return 1
13 % 2 回报 1
Next part is <>
which denotes Not equals.
下一部分是<>
表示不等于。
Therefor what your statement mean is Remainder of ID when it divided by 2 not equals to 0
因此,您的陈述的含义是 ID 除以 2 不等于 0 时的余数
Be careful because this is not going to work in Oracle database. Same Expression will be like below.
小心,因为这在 Oracle 数据库中不起作用。相同的表达式将如下所示。
MOD(ID, 2) <> 0
回答by Erwin Brandstetter
ID % 2
reduces all integer (monetary and numeric are allowed, too) numbers to 0 and 1 effectively.
Read about the modulo operatorin the manual.
回答by Saurabh
In oracle,
在甲骨文中,
select num from table where MOD (num, 2) = 0;
回答by Neha Chopra
dividend % divisor
股息 % 除数
Dividend is the numeric expression to divide. Dividend must be any expression of integer data type in sql server.
Dividend 是要除以的数值表达式。Dividend 必须是 sql server 中任何整数数据类型的表达式。
Divisor is the numeric expression to divide the dividend. Divisor must be expression of integer data type except in sql server.
Divisor 是用于划分被除数的数值表达式。除在 sql server 中外,除数必须是整数数据类型的表达式。
SELECT 15 % 2
Output
1
Dividend = 15
股息 = 15
Divisor = 2
除数 = 2
Let's say you wanted to query
假设您想查询
Query a list of CITY names from STATION with even ID numbers only.
仅使用偶数 ID 号从 STATION 查询城市名称列表。
Schema structure for STATION:
STATION 的架构结构:
ID Number
CITY varchar
STATE varchar
select CITY from STATION as st where st.id % 2 = 0
Will fetch the even set of records
In order to fetch the odd records with Id as odd number.
select CITY from STATION as st where st.id % 2 <> 0
% function reduces the value to either 0 or 1
% 函数将值减少到 0 或 1
回答by Icarus
It's taking the ID , dividing it by 2 and checking if the remainder is not zero; meaning, it's an odd ID.
它获取 ID ,将其除以 2 并检查余数是否不为零;意思是,这是一个奇怪的 ID。
回答by Madhurupa Moitra
<> means not equal. however, in some versions of SQL, you can write !=
<> 表示不相等。但是,在某些版本的 SQL 中,您可以编写 !=