如何將 MySQL SOUNDEX() 函式與 LIKE 運算子一起使用以從表中檢索記錄?


眾所周知,SOUNDEX() 函式用來返回聲碼,這是一種針對名稱進行索引的語音演算法,它根據英語讀音產生字串的字串。在以下示例中,我們從“student_info”表中獲取資料,並運用 SOUNDEX() 函式和 LIKE 運算子從表中檢索特定記錄 −

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101 | YashPal  | Amritsar   | History    |
| 105 | Gaurav   | Chandigarh | Literature |
| 125 | Raman    | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)

mysql> Select * from student_info where SOUNDEX(Name) LIKE '%G%';
+------+--------+------------+------------+
| id   | Name   | Address    | Subject    |
+------+--------+------------+------------+
| 105  | Gaurav | Chandigarh | Literature |
+------+--------+------------+------------+
1 row in set (0.00 sec)

我們已將列名用作 SOUNDEX() 函式的引數,並且它返回那些 SOUNDEX 值 LIKE %G% 的行。

請記住,SOUNDEX() 函式的輸出始終包含用作引數的字串的首字母。例如,如果我們將“Ram”作為 SOUNDEX() 函式的引數,那麼請看輸出,它包含“R”作為第一個字元 −

mysql> Select SOUNDEX('Ram');
+----------------+
| SOUNDEX('Ram') |
+----------------+
| R500           |
+----------------+
1 row in set (0.00 sec)

mysql> Select SOUNDEX('ram');
+----------------+
| SOUNDEX('ram') |
+----------------+
| R500           |
+----------------+
1 row in set (0.00 sec)

更新於:22-Jun-2020

248 次瀏覽

職業開啟您的

完成課程認證

開始學習
廣告
© . All rights reserved.