如何使用 MySQL SOUNDEX() 函式和 LIKE 運算子從表中檢索記錄?


眾所周知,SOUNDEX() 函式用於返回 SOUNDEX,一種用於索引按照英語發音讀起來聽起來一樣的名字的音標演算法,它是字串的字串。在以下示例中,我們從“student_info”表中獲取資料,並使用帶有 LIKE 運算子的 SOUNDEX() 函式從表中檢索特定記錄−

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)

更新於: 2020 年 6 月 22 日

248 次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.