在 MySQL 中的 varchar 欄位中搜索整數值?


要搜尋 varchar 檔案中的整數值,可以使用 CASE 語句。

我們先建立一個表格。我們有一個電子郵件列表 −

mysql> create table DemoTable
-> (
-> Title varchar(100)
-> );
Query OK, 0 rows affected (0.61 sec)

使用 insert 命令在表格中插入一些記錄 −

mysql> insert into DemoTable values('9John@example.com');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('3Carol@gmail.com');
Query OK, 1 row affected (0.45 sec)

使用 select 語句從表格中顯示所有記錄 −

mysql> select *from DemoTable;

輸出

這將產生以下輸出 −

+-------------------+
| Title             |
+-------------------+
| 9John@example.com |
| 3Carol@gmail.com  |
+-------------------+
2 rows in set (0.00 sec)

以下是在 MySQL 中的 varchar 欄位中搜索整數值的查詢 −

mysql> select (case when Title = 9 then 'Search Successful' else 'Search Unsuccessful' end)
AS Result from DemoTable;

輸出

這將產生以下輸出 −

+---------------------+
| Result              |
+---------------------+
| Search Successful   |
| Search Unsuccessful |
+---------------------+
2 rows in set, 2 warnings (0.00 sec)

更新於: 30-6 月-2020

196 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

立刻開始
廣告
© . All rights reserved.