sqlÖÐlikeÔõôÓÃ
like ÔËËã·ûÓÃÓÚÔÚ sql ÖоÙÐÐģʽƥÅäºÍÄ£ºýËÑË÷£¬¿ÉÒÔͨ¹ýÖ¸¶¨Ä£Ê½ËÑË÷Æ¥ÅäµÄÐС£Ä£Ê½°üÀ¨Í¨Åä·û£¬Èç %£¨Æ¥Åäí§Òâ×Ö·û£©¡¢_£¨Æ¥Åäµ¥¸ö×Ö·û£©¡¢[]£¨Æ¥Åä·½À¨ºÅÄÚ×Ö·û£©ºÍ [^]£¨Æ¥Åä²»º¬·½À¨ºÅÄÚ×Ö·û£©¡£like ÔËËã·ûÖ§³Öǰ׺¡¢ºó׺¡¢°üÀ¨ºÍ׼ȷƥÅ䣬²¢ÔÚÄ£ºýËÑË÷ÖÐʹÓà % ͨÅä·û¡£×¢ÖØ£¬¸ÃÔËËã·û²»Çø·Ö¾Þϸд£¬²¢ÇÒÆäÐÔÄÜÈ¡¾öÓÚģʽµÄÖØ´óÐÔ¡£
SQL ÖÐµÄ LIKE ÔËËã·û
LIKE ÔËËã·ûÊÇ SQL ÖÐÓÃÓÚģʽƥÅäºÍÄ£ºýËÑË÷µÄÔËËã·û¡£ËüÔÊÐíÄãÖ¸¶¨Ò»¸öģʽ»òÄ£°å£¬È»ºóËÑË÷Óë֮ƥÅäµÄÐС£
Óï·¨
SELECT ÁÐÃû FROM ±íÃû WHERE ÁÐÃû LIKE ģʽ
µÇ¼ºó¸´ÖÆ
ģʽ×é³É
%£ºÆ¥Åäí§ÒâÊýÄ¿µÄí§Òâ×Ö·û¡£
_£ºÆ¥Åäµ¥¸öí§Òâ×Ö·û¡£
[]£ºÆ¥Åä·½À¨ºÅÄÚµÄÈκε¥¸ö×Ö·û¡£
[^]£ºÆ¥Åä²»°üÀ¨·½À¨ºÅÄÚ×Ö·ûµÄÈκε¥¸ö×Ö·û¡£
Ó÷¨
LIKE ÔËËã·ûʹÓÃÒÔÏ·½·¨¾ÙÐÐÄ£ºýËÑË÷£º
ǰ׺ËÑË÷£ºÊ¹Óà “%” ºó׺µÄģʽ½«Æ¥ÅäÒÔ¸Ãģʽ¿ªÍ·µÄÖµ¡£ÀýÈ磬”LIKE ‘j%'” ½«Æ¥Åä “John”¡¢”James” µÈ¡£
ºó׺ËÑË÷£ºÊ¹Óà “%” ǰ׺µÄģʽ½«Æ¥ÅäÒÔ¸Ãģʽ×îºóµÄÖµ¡£ÀýÈ磬”LIKE ‘%son'” ½«Æ¥Åä “Johnson”¡¢”Nelson” µÈ¡£
°üÀ¨ËÑË÷£ºÊ¹Óà “%” ǰ׺ºÍºó׺µÄģʽ½«Æ¥Åä°üÀ¨¸ÃģʽµÄÖµ¡£ÀýÈ磬”LIKE ‘%on'” ½«Æ¥Åä “John”¡¢”Johnson”¡¢”Nelson” µÈ¡£
׼ȷƥÅ䣺Ҫ¾ÙÐÐ׼ȷƥÅ䣬¿ÉÒÔʹÓÃûÓÐͨÅä·ûµÄģʽ¡£ÀýÈ磬”LIKE ‘John'” ½«½öÆ¥Åä “John”¡£
ʾÀý
SELECT * FROM customers WHERE name LIKE 'Jo%'
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ«·µ»ØËùÓÐÒÔ “Jo” ¿ªÍ·µÄ¿Í»§ÐÕÃû¡£
SELECT * FROM products WHERE description LIKE '%computer%'
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ«·µ»ØËùÓÐÐÎòÖаüÀ¨ “computer” µÄ²úÆ·¡£
SELECT * FROM orders WHERE order_id LIKE '[a-z]%'
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ«·µ»ØËùÓÐÒÔСд×Öĸ¿ªÍ·µÄ¶©µ¥ ID¡£
×¢ÖØ
LIKE ÔËËã·û²»Çø·Ö¾Þϸд¡£
ÈôÊÇģʽÖÐûÓÐͨÅä·û£¬Ôò LIKE ÔËËã·û½«¾ÙÐÐ׼ȷƥÅä¡£
LIKE ÔËËã·ûµÄÐÔÄÜ¿ÉÄÜ»áÒòģʽµÄÖØ´óÐÔ¶øÒì¡£
ÒÔÉϾÍÊÇsqlÖÐlikeÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡