MySQL 中 WHERE 子句中的自動字串轉整型用於獲取特定的 id


如果字串以整數開頭,那麼它將字串轉換為整數,否則不會。我們首先建立一個 -

mysql> create table DemoTable1390
   -> (
   -> StudentId varchar(20)
   -> );
Query OK, 0 rows affected (0.93 sec)

使用 insert 在表中插入一些記錄 -

mysql> insert into DemoTable1390 values('563_John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1390 values('1001_Carol_Taylor');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1390 values('David_Miller_789');
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable1390 values('456_AdamSmith');
Query OK, 1 row affected (0.11 sec)

使用 select 顯示錶中的所有記錄 -

mysql> select * from DemoTable1390;

這將產生以下輸出 -

+-------------------+
| StudentId         |
+-------------------+
| 563_John          |
| 1001_Carol_Taylor |
| David_Miller_789  |
| 456_AdamSmith     |
+-------------------+
4 rows in set (0.00 sec)

以下是 where 子句中自動字串轉整型的查詢,用於獲取特定的 -

mysql> select * from DemoTable1390 where StudentId=456;

這將產生以下輸出 -

+---------------+
| StudentId     |
+---------------+
| 456_AdamSmith |
+---------------+
1 row in set, 4 warnings (0.02 sec)

更新日期: 11-Nov-2019

385 次瀏覽

開啟你的 職業

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.