如何在儲存為 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)

現在,假設如果我們想找出特定字串(比如“for email”)在儲存為該表格 'Purpose' 和 'Webaddress' 列記錄中的字串中的索引號,那麼以下查詢就可以做到 −

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”字串在第一行的第二個索引處。

更新於:2020-06-22

122 次瀏覽

開啟你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.