我正在尝试确定将数字与相同数字的字符或字符串版本进行比较的标准 SQL 行为。 SELECT 1 = '1'
(或类似的)是否总是返回某种“真实”值(true
、1
、 't'
等)?我已经在 PostgreSQL 和 MySQL 上确认了很多,但我找不到整个 SQL 的资源。
更新: 这个问题的目的是我试图弄清楚使用不带引号的数字在选择/插入/更新等时是否有效。来自值为数字的非数字字段。
最佳答案
SELECT 1='1'
给出 TRUE
自 '1'
是 INT
的正确构造函数在我所知的所有实现中。
但是 SQL 使用严格类型,请看:
# SELECT 1=CAST('1' AS TEXT);
ERROR: operator does not exist: integer = text
LINE 1: SELECT 1=CAST('1' AS TEXT);
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
关于标准(SQL 92、99 和 2003)似乎是错误的:
<literal> ::=
<signed numeric literal>
| <general literal>
<general literal> ::=
<character string literal>
| <national character string literal>
| <bit string literal>
| <hex string literal>
| <datetime literal>
| <interval literal>
<signed numeric literal> ::=
[ <sign> ] <unsigned numeric literal>
<unsigned numeric literal> ::=
<exact numeric literal>
| <approximate numeric literal>
<exact numeric literal> ::=
<unsigned integer> [ <period> [ <unsigned integer> ] ]
| <period> <unsigned integer>
<unsigned integer> ::= <digit>...
<character string literal> ::=
[ <introducer><character set specification> ]
<quote> [ <character representation>... ] <quote>
[ { <separator>... <quote> [ <character representation>... ] <quote> }... ]
因为<quote>
仅包含在 <bit string literal>
中, <hex string literal>
, ... 但不是数字文字...
关于sql - SQL 中 1 总是等于 '1' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2134102/