如何查詢儲存為 MySQL 表列中的記錄的字串的索引位置?


我們可以使用 FIELD() 函式來查詢儲存為 MySQL 表列中的記錄的字串的索引位置。為了演示這一點,我們使用名為“websites”的表,其具有以下資料

示例

mysql> Select * from websites;
+----+---------------+------------------------+
| Id | Purpose       | Webaddress             |
+----+---------------+------------------------+
| 1  | For tutorials | www.tutorialspoint.com |
| 2  | For searching | www.google.co.in       |
| 3  | For email     | www.gmail.com          |
+----+---------------+------------------------+
3 rows in set (0.00 sec)

現在,假設我們想從儲存在此表“Purpose”和“Webaddress”列中記錄的字串中找出特定字串(例如“for email”)的索引號,那麼以下查詢將執行此操作 −

mysql> Select FIELD('For email', purpose, webaddress) From websites;
+----------------------------------------+
| FIELD('For email', purpose, webaddress)|
+----------------------------------------+
|                                      0 |
|                                      0 |
|                                      1 |
+----------------------------------------+
3 rows in set (0.00 sec)

以上結果集顯示“For email”字串在第三行的第一個索引。

mysql> Select FIELD('www.tutorialspoint.com', purpose, web address) From websites;
+------------------------------------------------------+
| FIELD('www.tutorialspoint.com', purpose, web address)|
+------------------------------------------------------+
|                                                    2 |
|                                                    0 |
|                                                    0 |
+------------------------------------------------------+
3 rows in set (0.00 sec)

以上結果集顯示“www.tutorialspoint.com”字串在第一行的第二個索引。

更新於: 22-Jun-2020

122 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.