LIKE और Wildcards
In this page:
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
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
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
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
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
SELECT * FROM coupons WHERE code LIKE '50\%OFF' ESCAPE '\\';
- wildcards के साथ
=इस्तेमाल करना, जैसेemail = '%@gmail.com', जो literally compare करता है और कुछ भी match नहीं करता। %(कोई भी संख्या में characters) को_(exactly एक character) से confuse करना।- एक बड़े indexed column पर
'%gmail'जैसे%से एक pattern शुरू करना, जो index इस्तेमाल नहीं कर सकता और धीमा है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: