![](/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.