← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 5 of 8

LIKE और Wildcards

LIKE wildcards वाली एक search जैसा है, जैसे 'S से शुरू होने वाले names'। percent sign किसी भी letters के लिए खड़ा है।
Syntax
sql
WHERE column_name LIKE 'pattern'
-- %  matches any number of characters
-- _  matches exactly one character

Percent Wildcard

LIKE operator text columns में patterns search करने के लिए इस्तेमाल होता है, जैसे किसी specific domain पर खत्म होने वाला हर email ढूँढना। हम percent symbol को एक wildcard की तरह इस्तेमाल करते हैं। यह zero, एक, या कई characters represent करता है, इसलिए 'a%' letter a से शुरू होने वाली किसी भी चीज़ से match करता है।

उदाहरण: The Percent Wildcard

sql
SELECT * FROM users WHERE email LIKE '%@gmail.com';

Underscore Wildcard

Underscore character एक और wildcard है, तब उपयोगी जब आपको exactly पता हो कि कितने characters expect करने हैं। यह exactly एक single character represent करता है। यह तब perfect है जब आपको उस pattern की exact length पता हो जिसे आप match करना चाहते हैं, जैसे एक 4-digit product code।

उदाहरण: The Underscore Wildcard

sql
SELECT * FROM products WHERE code LIKE 'A_23';

NOT LIKE Operator

हम NOT LIKE इस्तेमाल कर सकते हैं ऐसी values ढूँढने के लिए जो हमारे pattern से match नहीं करतीं, जैसे एक known naming convention वाले test accounts filter out करना। यह आपके results से कुछ letters या domains वाले text को पूरी तरह exclude करने में मदद करता है।

उदाहरण: The NOT LIKE Operator

sql
SELECT * FROM users WHERE email NOT LIKE 'test%';

LIKE के साथ Case Sensitivity

Default रूप से, MySQL में LIKE operator standard collation के तहत case-insensitive है, इसलिए Apple और apple दोनों match करते हैं। अगर आपको एक exact case match search करना हो, आप byte-for-byte comparison force करने के लिए search string से पहले BINARY operator इस्तेमाल कर सकते हैं।

उदाहरण: Case Sensitivity with LIKE

sql
SELECT * FROM users WHERE name LIKE 'Apple'; -- matches 'apple' too, case-insensitive by default

Wildcards Escape करना

कभी-कभी आप wildcards की तरह इस्तेमाल करने के बजाय literal percentage या underscore characters search करना चाहते हैं, जैसे एक real percent sign रखने वाला discount code match करना।

हम एक custom escape character इस्तेमाल करके यह कर सकते हैं। यह MySQL को उन्हें pattern symbols के बजाय normal letters की तरह treat करने को कहता है।

उदाहरण: Escaping Wildcards

sql
SELECT * FROM coupons WHERE code LIKE '50\%OFF' ESCAPE '\\';
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. wildcards के साथ = इस्तेमाल करना, जैसे email = '%@gmail.com', जो literally compare करता है और कुछ भी match नहीं करता।
  2. % (कोई भी संख्या में characters) को _ (exactly एक character) से confuse करना।
  3. एक बड़े indexed column पर '%gmail' जैसे % से एक pattern शुरू करना, जो index इस्तेमाल नहीं कर सकता और धीमा है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.