LIKE ऑपरेटर और वाइल्डकार्ड
आंशिक मिलान के लिए LIKE ऑपरेटर का उपयोग करें। दो मुख्य वाइल्डकार्ड हैं:
%- शून्य या अधिक वर्णों से मेल खाता है_(अंडरस्कोर) - ठीक एक वर्ण से मेल खाता है
उदाहरण: टेबल बनाना और डेटा इन्सर्ट करना
mysql> CREATE TABLE DemoTable806(
StudentId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
StudentName VARCHAR(100),
StudentSubject VARCHAR(100)
);
Query OK, 0 rows affected (0.57 sec)
रिकॉर्ड्स इन्सर्ट करना
mysql> INSERT INTO DemoTable806(StudentName,StudentSubject) VALUES('Chris','Java in Depth With Data Structure');
Query OK, 1 row affected (0.15 sec)
mysql> INSERT INTO DemoTable806(StudentName,StudentSubject) VALUES('Robert','Introduction to MySQL');
Query OK, 1 row affected (0.14 sec)
mysql> INSERT INTO DemoTable806(StudentName,StudentSubject) VALUES('Bob','C++ in Depth With Data Structure And Algorithm');
Query OK, 1 row affected (0.14 sec)
mysql> INSERT INTO DemoTable806(StudentName,StudentSubject) VALUES('Adam','Introduction to MongoDB');
Query OK, 1 row affected (0.11 sec)
सभी रिकॉर्ड्स देखना
mysql> SELECT * FROM DemoTable806;
+-----------+-------------+------------------------------------------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+------------------------------------------------+ | 1 | Chris | Java in Depth With Data Structure | | 2 | Robert | Introduction to MySQL | | 3 | Bob | C++ in Depth With Data Structure And Algorithm | | 4 | Adam | Introduction to MongoDB | +-----------+-------------+------------------------------------------------+ 4 rows in set (0.00 sec)
आंशिक मिलान क्वेरी
'Depth' शब्द वाले विषय खोजने के लिए:
mysql> SELECT * FROM DemoTable806 WHERE StudentSubject LIKE '%Depth%';
परिणाम
+-----------+-------------+------------------------------------------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+------------------------------------------------+ | 1 | Chris | Java in Depth With Data Structure | | 3 | Bob | C++ in Depth With Data Structure And Algorithm | +-----------+-------------+------------------------------------------------+ 2 rows in set (0.00 sec)
LIKE पैटर्न के उदाहरण
| पैटर्न | विवरण |
|---|---|
'%abc' | 'abc' से समाप्त होने वाले मान |
'abc%' | 'abc' से शुरू होने वाले मान |
'%abc%' | कहीं भी 'abc' वाले मान |
'_abc' | एक वर्ण के बाद 'abc' वाले मान |
'a_c' | 'a' से शुरू, कोई भी वर्ण, फिर 'c' वाले मान |
केस सेंसिटिविटी
डिफ़ॉल्ट रूप से MySQL में LIKE केस-इनसेंसिटिव होता है। केस-सेंसिटिव खोज के लिए BINARY कीवर्ड उपयोग करें:
SELECT * FROM DemoTable806 WHERE StudentSubject LIKE BINARY '%depth%';
प्रदर्शन नोट
लीडिंग वाइल्डकार्ड (%text) इंडेक्स का उपयोग नहीं कर सकते। पूर्ण-पाठ खोज के लिए FULLTEXT INDEX और MATCH() AGAINST() का उपयोग करें।