[英]Searching with a column that has `|` character as its value in mysql
With a
as a column in table b
, I'd like to understand why the search fetches a row for a=0
condition! 随着
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)
Thanks in advance! 提前致谢!
This is a result of how MySQL casts text/varchar to integer. 这是MySQL如何将text / varchar转换为整数的结果。
select *, cast(ba as unsigned) from (select "None|679501|3371371|0" as a) b where a=0
gives 0
for the second column. 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
。
If you cast the integer to text, then you get 0 rows as expected: select * from (select "None|679501|3371371|0" as a) b where a='0'
如果将整数转换为文本,则将按预期方式获得0行:
select * from (select "None|679501|3371371|0" as a) b where a='0'
The results have nothing to do with the fact that the delimiter you are using is |
结果与您使用的定界符为
|
无关|
. 。 It would be the same with any non-numeric character.
任何非数字字符都将相同。 Likewise, neither
null
nor None
are special in this context. 同样,在这种情况下,
null
和None
是特殊的。 That could be any string. 那可以是任何字符串。
In the expression, 0="0|679501|3371371|0"
, MySQL is doing a "string to int" on the string and comparing with 0. It's behavior is similar to how atoi
in C works. 在表达式
0="0|679501|3371371|0"
,MySQL在字符串上执行了“从字符串到整数”的操作,并与0进行比较。它的行为类似于C语言中atoi
的工作方式。 Parsing stops at the first non-numeric character. 解析在第一个非数字字符处停止。 If the string doesn't start with a numeric character, then it yields 0.
如果字符串不是以数字字符开头,那么它将产生0。
You can simplify examining the behavior with the following queries: 您可以使用以下查询简化行为的检查:
> select 0="0|1|2";
+-----------+
| 0="0|1|2" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
"0|1|2"
converted to an integer is 0. Parsing stops at |
转换为整数的
"0|1|2"
为0 |
. 。 Comparing 0=0 gives 1.
比较0 = 0得出1。
> select 0="0x1x2";
+-----------+
| 0="0x1x2" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
"0x1x2"
converted to an integer is 0. Parsing stops at |
转换为整数的
"0x1x2"
为0 |
. 。 Comparing 0=0 gives 1.
比较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"
converted to an integer is 1. Parsing stops at |
转换为整数的
"1|2|0"
为1 |
. 。 Comparing 0=1 gives 0.
比较0 = 1得出0。
> select 1="1x2x0";
+-----------+
| 1="1x2x0" |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
"1x2x0"
converted to an integer is 1. Parsing stops at |
转换为整数的
"1x2x0"
为1 |
. 。 Comparing 1=1 gives 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"
converted to an integer is 0 since the string doesn't start with a numeric and parsing stops immediately. 转换为整数的
"null|1|2"
为0,因为字符串不是以数字开头,并且解析立即停止。 Default value is 0. Comparing 0=0 gives 1. 默认值为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"
converted to an integer is 0 since the string doesn't start with a numeric and parsing stops immediately. 转换为整数的
"foo|1|2"
为0,因为字符串不是以数字开头,并且解析立即停止。 Default value is 0. Comparing 0=0 gives 1. 默认值为0。比较0 = 0得出1。
This is due to an implicit type conversion according to operands coercibilities . 这是由于根据操作数的强制性进行了隐式类型转换。 Your first query:
您的第一个查询:
select * from (select "0|679501|3371371|0" as a) b where a=0;
returns a row since 0|679501|3371371|0
has a numeric character ( 0
) in its beginning which is equal to the other side of comparison on conversion but this: 返回行,因为
0|679501|3371371|0
的开头具有数字字符( 0
),它等于转换时比较的另一侧,但这是:
select * from (select "9|679501|3371371|0" as a) b where a=0;
returns null. 返回null。 MySQL automatically converts numbers to strings as necessary, and vice versa.
MySQL会根据需要自动将数字转换为字符串,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.