ใช้ Regular Expression กับ REGEXP เพื่อค้นหาคำที่ตรงพอดี (whole word) ไม่ใช่ส่วนหนึ่งของคำอื่น
ไวยากรณ์
SELECT * FROM your_table
WHERE your_column REGEXP '(^|[[:space:]])คำที่ค้นหา([[:space:]]|$)';
สร้างตารางและเพิ่มข้อมูล
mysql> CREATE TABLE DemoTable (Title TEXT);
mysql> INSERT INTO DemoTable VALUES
('This is the Introduction to Java'),
('This is the Introduction to MongoDB'),
('This is the Introduction to MySQL');
ดูข้อมูล
mysql> SELECT * FROM DemoTable;
+-------------------------------------+
| Title |
+-------------------------------------+
| This is the Introduction to Java |
| This is the Introduction to MongoDB |
| This is the Introduction to MySQL |
+-------------------------------------+
ค้นหาคำว่า 'MySQL' ที่เป็นคำเต็ม
mysql> SELECT * FROM DemoTable
-> WHERE Title REGEXP '(^|[[:space:]])MySQL([[:space:]]|$)';
ผลลัพธ์
+-----------------------------------+
| Title |
+-----------------------------------+
| This is the Introduction to MySQL |
+-----------------------------------+
คำอธิบาย Regex:
(^|[[:space:]])— เริ่มต้นข้อความ หรือ ช่องว่างMySQL— คำที่ต้องการค้นหา([[:space:]]|$)— ช่องว่าง หรือ จบข้อความ
จะไม่จับ 'MySQLDatabase' หรือ 'BestMySQL' เพราะไม่มีขอบเขตคำ