我试图确定用于将数字与相同数字的字符或字符串版本进行比较的标准SQL行为。是否SELECT 1 = '1'(或类似)总是返回某种“truthy”值(true,1,'t'等)?我已经在PostgreSQL和MySQL上进行了同样的确认,但是我找不到整个SQL的资源。
SELECT 1 = '1'
true
1
't'
更新:问题 的目的是我试图找出在选择/插入/更新/等时使用不带引号的数字是否可以使用。来自其值为数字的非数字字段。
SELECT 1='1'给人TRUE因为'1'是一个正确的构造函数INT中知道我所有的实现。
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>…中,但不包含在数字文字中。
<quote>
<bit string literal>
<hex string literal>