如何檢查特定國家/地區程式碼是否存在於包含 MySQL 的單元格中?


對於特定值,使用 FIND_IN_SET()。我們首先建立 −

mysql> create table DemoTable1439
   -> (
   -> CountryId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CountryCode varchar(20)
   -> );
Query OK, 0 rows affected (0.49 sec)

在表中插入一些記錄,使用插入 −

mysql> insert into DemoTable1439(CountryCode) values('1022_US,7894_UK');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1439(CountryCode) values('6567_AUS,7894_UK');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1439(CountryCode) values('6567_AUS');
Query OK, 1 row affected (0.09 sec)

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

mysql> select * from DemoTable1439;

將輸出以下結果 −

+-----------+------------------+
| CountryId | CountryCode      |
+-----------+------------------+
|         1 | 1022_US,7894_UK  |
|         2 | 6567_AUS,7894_UK |
|         3 | 6567_AUS         |
+-----------+------------------+
3 rows in set (0.00 sec)

以下查詢用於檢查國家/地區程式碼是否存在於單元格中 −

mysql> select * from DemoTable1439
   -> where find_in_set('6567_AUS',CountryCode) > 0;

將輸出以下結果 −

+-----------+------------------+
| CountryId | CountryCode      |
+-----------+------------------+
|         2 | 6567_AUS,7894_UK |
|         3 | 6567_AUS         |
+-----------+------------------+
2 rows in set (0.00 sec)

更新於:12-Nov-2019

218 瀏覽

開啟你的 職業

完成課程,獲得認證

開始
廣告