如何使用 MySQL SOUNDEX() 函数和 LIKE 运算符从表中检索记录?

mysqlmysqli database

众所周知,SOUNDEX() 函数用于返回 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)

相关文章