![](/img/trans.png)
[英]Mysql - Check if VARCHAR column has a missing value on its incrementation
[英]Searching with a column that has `|` character as its value in mysql
随着a
在表中的列b
,我想明白为什么搜索取出一行对a=0
的条件!
mysql> select * from (select "0|679501|3371371|0" as a) b where a=0;
+--------------------+
| a |
+--------------------+
| 0|679501|3371371|0 |
+--------------------+
1 row in set, 2 warnings (0.00 sec)
mysql> select * from (select "079501|3371371|0" as a) b where a=0;
Empty set, 1 warning (0.04 sec)
mysql> select * from (select "None|679501|3371371|0" as a) b where a=0;
+-----------------------+
| a |
+-----------------------+
| None|679501|3371371|0 |
+-----------------------+
1 row in set, 2 warnings (0.00 sec)
mysql> select * from (select "null|679501|3371371|0" as a) b where a=0;
+-----------------------+
| a |
+-----------------------+
| null|679501|3371371|0 |
+-----------------------+
1 row in set, 2 warnings (0.00 sec)
mysql> select * from (select "679501|null|3371371|0" as a) b where a=0;
Empty set, 1 warning (0.01 sec)
提前致谢!
这是MySQL如何将text / varchar转换为整数的结果。
select *, cast(ba as unsigned) from (select "None|679501|3371371|0" as a) b where a=0
表示第二select *, cast(ba as unsigned) from (select "None|679501|3371371|0" as a) b where a=0
0
。
如果将整数转换为文本,则将按预期方式获得0行: select * from (select "None|679501|3371371|0" as a) b where a='0'
结果与您使用的定界符为|
无关|
。 任何非数字字符都将相同。 同样,在这种情况下, null
和None
是特殊的。 那可以是任何字符串。
在表达式0="0|679501|3371371|0"
,MySQL在字符串上执行了“从字符串到整数”的操作,并与0进行比较。它的行为类似于C语言中atoi
的工作方式。 解析在第一个非数字字符处停止。 如果字符串不是以数字字符开头,那么它将产生0。
您可以使用以下查询简化行为的检查:
> select 0="0|1|2";
+-----------+
| 0="0|1|2" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"0|1|2"
为0 |
。 比较0 = 0得出1。
> select 0="0x1x2";
+-----------+
| 0="0x1x2" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"0x1x2"
为0 |
。 比较0 = 0得出1。
> select 0="1|2|0";
+-----------+
| 0="1|2|0" |
+-----------+
| 0 |
+-----------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"1|2|0"
为1 |
。 比较0 = 1得出0。
> select 1="1x2x0";
+-----------+
| 1="1x2x0" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"1x2x0"
为1 |
。 比较1 = 1得出1。
> select 0="null|1|2";
+--------------+
| 0="null|1|2" |
+--------------+
| 1 |
+--------------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"null|1|2"
为0,因为字符串不是以数字开头,并且解析立即停止。 默认值为0。比较0 = 0得出1。
> select 0="foo|1|2";
+-------------+
| 0="foo|1|2" |
+-------------+
| 1 |
+-------------+
1 row in set, 1 warning (0.00 sec)
转换为整数的"foo|1|2"
为0,因为字符串不是以数字开头,并且解析立即停止。 默认值为0。比较0 = 0得出1。
这是由于根据操作数的强制性进行了隐式类型转换。 您的第一个查询:
select * from (select "0|679501|3371371|0" as a) b where a=0;
返回行,因为0|679501|3371371|0
的开头具有数字字符( 0
),它等于转换时比较的另一侧,但这是:
select * from (select "9|679501|3371371|0" as a) b where a=0;
返回null。 MySQL会根据需要自动将数字转换为字符串,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.