SQL 选择除以 2 后余数(模)为 1 的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3756928/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:36:27  来源:igfitidea点击:

Selecting rows where remainder (modulo) is 1 after division by 2?

sqldatabaseselectwhere-clausemodulus

提问by Carlos

There is a column in options that hold an integer. I want to select the row only if that value % 2 = 1.

选项中有一个包含整数的列。我只想在该值 % 2 = 1 时选择该行。

I know this can be done in 2 queries but is it possible to do it in 1?

我知道这可以在 2 个查询中完成,但是否可以在 1 个中完成?

回答by OMG Ponies

MySQL, SQL Server, PostgreSQL, SQLite support using the percent sign as the modulus:

MySQL、SQL Server、PostgreSQL、SQLite 支持使用百分号作为模数:

WHERE column % 2 = 1

For Oracle, you have to use the MOD function:

对于 Oracle,您必须使用MOD 功能

WHERE MOD(column, 2) = 1

回答by Jonathan Leffler

At least some versions of SQL (Oracle, Informix, DB2, ISO Standard) support:

至少某些版本的 SQL(Oracle、Informix、DB2、ISO 标准)支持:

WHERE MOD(value, 2) = 1

MySQL supports '%' as the modulus operator:

MySQL 支持 '%' 作为模运算符:

WHERE value % 2 = 1

回答by einarmagnus

select * from table where value % 2 = 1works fine in mysql.

select * from table where value % 2 = 1在 mysql 中工作正常。

回答by David R Tribble

Note:Disregard this answer, as I must have misunderstood the question.

注意:忽略这个答案,因为我一定误解了这个问题。

select *
  from Table
  where len(ColName) mod 2 = 1

The exact syntax depends on what flavor of SQL you're using.

确切的语法取决于您使用的 SQL 风格。