LIKE ऑपरेटर के साथ % वाइल्डकार्ड का उपयोग करें
किसी विशिष्ट सबस्ट्रिंग से समाप्त होने वाले मानों को खोजने (और अपडेट करने) के लिए LIKE '%.substring' पैटर्न का उपयोग करें।
सिंटैक्स
UPDATE yourTableName SET yourColumnName = 'newValue' WHERE yourColumnName LIKE '%.yourString';
व्यावहारिक उदाहरण: .docx को .pdf में बदलना
टेबल बनाना
mysql> CREATE TABLE RowEndsWithSpecificString (
Id INT NOT NULL AUTO_INCREMENT,
FileName VARCHAR(30),
PRIMARY KEY(Id)
);
फाइल नाम डालना
mysql> INSERT INTO RowEndsWithSpecificString(FileName) VALUES('MergeSort.c');
mysql> INSERT INTO RowEndsWithSpecificString(FileName) VALUES('BubbleSortIntroduction.pdf');
mysql> INSERT INTO RowEndsWithSpecificString(FileName) VALUES('AllMySQLQuery.docx');
mysql> INSERT INTO RowEndsWithSpecificString(FileName) VALUES('JavaCollections.pdf');
mysql> INSERT INTO RowEndsWithSpecificString(FileName) VALUES('JavaServlet.pdf');
अपडेट से पहले
+----+----------------------------+ | Id | FileName | +----+----------------------------+ | 1 | MergeSort.c | | 2 | BubbleSortIntroduction.pdf | | 3 | AllMySQLQuery.docx | | 4 | JavaCollections.pdf | | 5 | JavaServlet.pdf | +----+----------------------------+
क्वेरी: .docx से समाप्त होने वाले को .pdf में अपडेट करना
mysql> UPDATE RowEndsWithSpecificString SET FileName = 'IntroductionToCoreJava.pdf' WHERE FileName LIKE '%.docx';
Query OK, 1 row affected (0.14 sec) Rows matched: 1 Changed: 1 Warnings: 0
अपडेट के बाद
+----+----------------------------+ | Id | FileName | +----+----------------------------+ | 1 | IntroductionToCoreJava.pdf | | 2 | BubbleSortIntroduction.pdf | | 3 | IntroductionToCoreJava.pdf | | 4 | JavaCollections.pdf | | 5 | JavaServlet.pdf | +----+----------------------------+
पैटर्न विविधताएं
| पैटर्न | मिलान | उदाहरण |
|---|---|---|
LIKE 'abc%' | 'abc' से शुरू | 'abc123', 'abc' |
LIKE '%abc' | 'abc' से समाप्त | '123abc', 'abc' |
LIKE '%abc%' | 'abc' कहीं भी | '123abc456', 'abc' |
LIKE '_abc' | एक अक्षर फिर 'abc' | 'xabc', '1abc' |
केस-सेंसिटिविटी
- डिफ़ॉल्ट कोलेशन (जैसे
utf8mb4_general_ci) केस-इनसेंसिटिव है। - केस-सेंसिटिव मिलान के लिए:
WHERE FileName LIKE BINARY '%.docx'याCOLLATE utf8mb4_binउपयोग करें।
नोट
उदाहरण में सभी .docx फाइलों को एक ही नाम 'IntroductionToCoreJava.pdf' में बदल दिया गया। वास्तविक उपयोग में, आप REPLACE(FileName, '.docx', '.pdf') का उपयोग करके केवल एक्सटेंशन बदलना चाहेंगे।