简体   繁体   English

SQL中1总是等于'1'吗?

[英]Does 1 always equal '1' in SQL?

I am trying to determine that standard SQL behaviour for comparing a number to a character or string version of the same number. 我试图确定用于将数字与相同数字的字符或字符串版本进行比较的标准SQL行为。 Does SELECT 1 = '1' (or the like) always return some sort of "truthy" value ( true , 1 , 't' , etc.)? SELECT 1 = '1' (或类似)是否总是返回某种“真实”值( true1't'等)? I have confirmed as much on PostgreSQL and MySQL, but I cannot find a resource for SQL as a whole. 我已经在PostgreSQL和MySQL上做了很多确认,但我找不到整个SQL的资源。

Update: The purpose for the question is that I'm trying to figure out if using a number, without the quotes, will work when selecting/inserting/updating/etc. 更新:问题的目的是我试图弄清楚在选择/插入/更新/等时使用不带引号的数字是否有效。 from a non-numeric field whose value is a number. 来自非数字字段,其值为数字。

SELECT 1='1' gives TRUE since '1' is a correct constructor for INT in all implementation known to me. SELECT 1='1'给出TRUE因为在我知道的所有实现中, '1'INT的正确构造函数。

But SQL uses strict typing, see that: 但是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.

Regarding the standard (SQL 92, 99 & 2003) it seems to be wrong: 关于标准(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> }... ]

because <quote> is only contained in <bit string literal> , <hex string literal> , ... but not in numeric literals... 因为<quote>仅包含在<bit string literal><hex string literal> ,...中,但不包含在数字文字中...

SQL Server SQL Server

if 1 = '1'
print 'yes'
else
print 'no'

output: yes 输出:是的

This gets converted, see here for a whole list of implicit and explicit conversion possibilities: CAST and CONVERT (Transact-SQL) 这会被转换,请参阅此处获取隐式和显式转换可能性的完整列表: CAST和CONVERT(Transact-SQL)

First off, in SQL Server SELECT 1 = '1' isn't valid. 首先,在SQL Server SELECT 1 = '1'无效。 Although, if you run the following code, you'll find that 1 does = '1' 虽然,如果你运行以下代码,你会发现1做='1'

if (1 = '1') begin
    print 'true'
end else begin
    print 'false'
end

results: 结果:

true

" SQL in general" does not have concept of a "truthy" value. “一般的SQL ”没有“truthy”值的概念。

Unlike MySQL and PostgreSQL , in Oracle and SQL Server , no internal datatypes can be used as boolean values in WHERE clauses or WHEN predicates. MySQLPostgreSQL不同,在OracleSQL Server ,没有内部数据类型可以用作WHERE子句或WHEN谓词中的布尔值。

You should always have some kind of a predicate to use in these clauses. 你应该总是在这些子句中使用某种谓词。

No datatype can be used in mydata to make these queries work: mydata没有数据类型可用于使这些查询起作用:

SELECT  1
WHERE   @mydata

or 要么

SELECT  1
FROM    dual
WHERE   :mydata

Also, no SQL standard prescribes the type casting order. 此外,没有SQL标准规定了类型转换顺序。

The datatype of the constant can be casted to that of that of the column datatype or vice versa. 常量的数据类型可以转换为列数据类型的数据类型,反之亦然。

This can lead to the problems similar to those described in this question . 这可能导致类似于本问题中描述的问题

1 is an Number and '1' is a CHAR array of some sort, they should never be equal. 1是数字,'1'是某种类型的CHAR数组,它们永远不应该相等。 If they are that is an implementation dependent behavior 如果它们是依赖于实现的行为

Testing from MySQL 5.x and SQL Server 2005, they both perform implicit conversion of '1' into 1 for the evaluation to return true. 从MySQL 5.x和SQL Server 2005进行测试,它们都将'1'隐式转换为1以使评估返回true。

But that could also have to do with collation. 但这也可能与整理有关。

虽然它似乎在许多实现中有效,但根据SQL标准,不允许比较1 = '1'

The question (given the update text) is not if: 问题(给定更新文本)不是:

SELECT 1 = '1'

will work, but will: 会工作,但会:

SELECT '1'::text = 1

work. 工作。 Which is of course: no. 这当然是:不。 At the very least on PostgreSQL, and for a really good reason. 至少在PostgreSQL上,并且有一个很好的理由。

From the Update: 来自更新:

Your update sounds like you want to do the following: 您的更新听起来像是要执行以下操作:

SELECT *
FROM MyTable 
WHERE StringColumn = 1

That will not work. 那不管用。 If you have ANY values in that string column which are non-numeric, as soon as the sql engine gets to that row it will throw an error. 如果该字符串列中的任何值都是非数字的,则只要sql引擎到达该行,就会抛出错误。 In MS SQL Server the error is " Conversion failed when converting the varchar value 'blah' to data type int. " 在MS SQL Server中,错误是“ 将varchar值'blah'转换为数据类型int时转换失败。

So, if you want to do the comparison, you would have to make sure you are comparing like data types. 因此,如果要进行比较,则必须确保比较类似的数据类型。 For example: 例如:

SELECT *
FROM MyTable 
WHERE StringColumn = '1'

For an "always true" select statement simply use SELECT 1 . 对于“始终为true”的select语句,只需使用SELECT 1 That will always be true. 这将永远是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM