• <small id='QfbpB'></small><noframes id='QfbpB'>

  • <legend id='QfbpB'><style id='QfbpB'><dir id='QfbpB'><q id='QfbpB'></q></dir></style></legend>
  • <tfoot id='QfbpB'></tfoot>
    <i id='QfbpB'><tr id='QfbpB'><dt id='QfbpB'><q id='QfbpB'><span id='QfbpB'><b id='QfbpB'><form id='QfbpB'><ins id='QfbpB'></ins><ul id='QfbpB'></ul><sub id='QfbpB'></sub></form><legend id='QfbpB'></legend><bdo id='QfbpB'><pre id='QfbpB'><center id='QfbpB'></center></pre></bdo></b><th id='QfbpB'></th></span></q></dt></tr></i><div id='QfbpB'><tfoot id='QfbpB'></tfoot><dl id='QfbpB'><fieldset id='QfbpB'></fieldset></dl></div>

        • <bdo id='QfbpB'></bdo><ul id='QfbpB'></ul>

        mysql:为什么将“字符串"与 0 进行比较会给出正确的结果?

        mysql: why comparing a #39;string#39; to 0 gives true?(mysql:为什么将“字符串与 0 进行比较会给出正确的结果?)

            • <bdo id='avWf0'></bdo><ul id='avWf0'></ul>
              • <i id='avWf0'><tr id='avWf0'><dt id='avWf0'><q id='avWf0'><span id='avWf0'><b id='avWf0'><form id='avWf0'><ins id='avWf0'></ins><ul id='avWf0'></ul><sub id='avWf0'></sub></form><legend id='avWf0'></legend><bdo id='avWf0'><pre id='avWf0'><center id='avWf0'></center></pre></bdo></b><th id='avWf0'></th></span></q></dt></tr></i><div id='avWf0'><tfoot id='avWf0'></tfoot><dl id='avWf0'><fieldset id='avWf0'></fieldset></dl></div>

                    <tbody id='avWf0'></tbody>

                  <small id='avWf0'></small><noframes id='avWf0'>

                  <tfoot id='avWf0'></tfoot>
                • <legend id='avWf0'><style id='avWf0'><dir id='avWf0'><q id='avWf0'></q></dir></style></legend>
                  本文介绍了mysql:为什么将“字符串"与 0 进行比较会给出正确的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在做一些 MySQL 测试查询,并意识到将字符串列与 0(作为数字)进行比较会得到 TRUE

                  I was doing some MySQL test queries, and realized that comparing a string column with 0 (as a number) gives TRUE!

                  select 'string' = 0 as res; -- res = 1 (true), UNexpected! why!??!?!
                  

                  但是,将它与任何其他数字,正数或负数,整数或小数进行比较,会按预期给出 false(当然,除非字符串是将数字表示为字符串)

                  however, comparing it to any other number, positive or negative, integer or decimal, gives false as expected (of course unless the string is the representation of the number as string)

                  select 'string' = -12 as res; -- res = 0 (false), expected
                  select 'string' = 3131.7 as res; -- res = 0 (false), expected
                  select '-12' = -12 as res; -- res = 1 (true), expected
                  

                  当然,将字符串与 '0' 作为字符串进行比较,如预期的那样给出错误.

                  Of course comparing the string with '0' as string, gives false, as expected.

                  select 'string' = '0' as res; -- res = 0 (false), expected
                  

                  但是为什么它对 'string' = 0 给出 true ?

                  but why does it give true for 'string' = 0 ?

                  这是为什么?

                  推荐答案

                  MySQL 自动将字符串转换为数字:

                  MySQL automatically casts a string to a number:

                  SELECT '1string' = 0 AS res; -- res = 0 (false)
                  SELECT '1string' = 1 AS res; -- res = 1 (true)
                  SELECT '0string' = 0 AS res; -- res = 1 (true)
                  

                  并且不以数字开头的字符串被评估为 0:

                  and a string that does not begin with a number is evaluated as 0:

                  SELECT 'string' = 0 AS res;  -- res = 1 (true)
                  

                  当然,当我们尝试将一个字符串与另一个字符串进行比较时,没有转换:

                  Of course, when we try to compare a string with another string there's no conversion:

                  SELECT '0string' = 'string' AS res; -- res = 0 (false)
                  

                  但是我们可以使用例如 + 运算符来强制转换:

                  but we can force a conversion using, for example, a + operator:

                  SELECT '0string' + 0 = 'string' AS res; -- res = 1 (true)
                  

                  最后一个查询返回 TRUE 因为我们将字符串 '0string' 与数字 0 相加,所以字符串必须转换为数字,它变成 SELECT 0 + 0 = 'string' 和然后再次将字符串 'string' 转换为数字,然后再与 0 进行比较,然后变为 SELECT 0 = 0 为 TRUE.

                  last query returns TRUE because we ar summing a string '0string' with a number 0, so the string has to be converted to a number, it becomes SELECT 0 + 0 = 'string' and then again the string 'string' is converted to a number before being compared to 0, and it then becomes SELECT 0 = 0 which is TRUE.

                  这也可以:

                  SELECT '1abc' + '2ef' AS total; -- total = 1+2 = 3
                  

                  并将返回转换为数字的字符串的总和(在本例中为 1 + 2).

                  and will return the sum of the strings converted to numbers (1 + 2 in this case).

                  这篇关于mysql:为什么将“字符串"与 0 进行比较会给出正确的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
                    <tbody id='3I3US'></tbody>
                  <legend id='3I3US'><style id='3I3US'><dir id='3I3US'><q id='3I3US'></q></dir></style></legend>

                    <small id='3I3US'></small><noframes id='3I3US'>

                    1. <i id='3I3US'><tr id='3I3US'><dt id='3I3US'><q id='3I3US'><span id='3I3US'><b id='3I3US'><form id='3I3US'><ins id='3I3US'></ins><ul id='3I3US'></ul><sub id='3I3US'></sub></form><legend id='3I3US'></legend><bdo id='3I3US'><pre id='3I3US'><center id='3I3US'></center></pre></bdo></b><th id='3I3US'></th></span></q></dt></tr></i><div id='3I3US'><tfoot id='3I3US'></tfoot><dl id='3I3US'><fieldset id='3I3US'></fieldset></dl></div>
                        <bdo id='3I3US'></bdo><ul id='3I3US'></ul>
                        <tfoot id='3I3US'></tfoot>