sqlÖÐlikeÓï¾äÔõôÓÃ
like Óï¾äÓÃÓÚÔÚ sql ÖÐƾ֤ģʽƥÅä×Ö·û»ò×Ö·û´®£¬Ó﷨Ϊ£ºselect column_name from table_name where column_name like ‘pattern’¡£ËüʹÓà % Æ¥ÅäÁã¸ö»ò¶à¸ö×Ö·û¡¢_ Æ¥Åäµ¥¸ö×Ö·û¡¢[ ] Æ¥Åä·½À¨ºÅÄÚ×Ö·û¡¢^ Æ¥Åä³ýÖ¸¶¨×Ö·ûÍâµÄÈκÎ×Ö·û¡£Ä¬ÈϾÞϸдÃô¸Ð£¬¿ÉʹÓà collate ×Ó¾ä¾ÙÐоÞϸд²»Ãô¸ÐÆ¥Åä¡£
SQL ÖÐ LIKE Óï¾äµÄÓ÷¨
LIKE Óï¾äµÄ×÷ÓÃ
LIKE Óï¾äÓÃÓÚÔÚ SQL ÅÌÎÊÖÐƾ֤ģʽƥÅä×Ö·û»ò×Ö·û´®¡£ËüÔÊÐíÄú²éÕÒ°üÀ¨Ìض¨×Ö·ûÐòÁлòÇкÏÌض¨Ä£Ê½µÄÖµ¡£
Óï·¨
SELECT column_name FROM table_name WHERE column_name LIKE 'pattern';
µÇ¼ºó¸´ÖÆ
ģʽƥÅä·û
LIKE Óï¾äʹÓÃÒÔÏÂÌØÊâ×Ö·û×÷ΪģʽƥÅä·û£º
%: Æ¥ÅäÁã¸ö»ò¶à¸ö×Ö·û¡£
_: Æ¥ÅäÈκε¥¸ö×Ö·û¡£
[ ]: Æ¥Åä·½À¨ºÅÄÚµÄÈκÎÒ»¸ö×Ö·û¡£
1: Æ¥Åä·½À¨ºÅÄÚ³ýÖ¸¶¨×Ö·ûÖ®ÍâµÄÈκÎÒ»¸ö×Ö·û¡£
ʾÀý
²éÕÒÒÔ “A” ¿ªÍ·µÄµ¥´Ê£º
SELECT word FROM dictionary WHERE word LIKE 'A%';
µÇ¼ºó¸´ÖÆ
²éÕÒ°üÀ¨ “ing” µÄµ¥´Ê£º
SELECT word FROM dictionary WHERE word LIKE '%ing';
µÇ¼ºó¸´ÖÆ
²éÕÒ²»ÒÔ “x” ×îºóµÄµ¥´Ê£º
SELECT word FROM dictionary WHERE word NOT LIKE '%x';
µÇ¼ºó¸´ÖÆ
²éÕÒ°üÀ¨Êý×ֵĵ¥´Ê£º
SELECT word FROM dictionary WHERE word LIKE '%[0-9]%';
µÇ¼ºó¸´ÖÆ
¾ÞϸдÃô¸ÐÐÔ
ĬÈÏÇéÐÎÏ£¬LIKE Óï¾äÊǾÞϸдÃô¸ÐµÄ¡£Òª¾ÙÐоÞϸд²»Ãô¸ÐµÄÆ¥Å䣬ÇëʹÓà COLLATE ×Ӿ䡣
ʾÀý£º¾Þϸд²»Ãô¸ÐµÄÆ¥Åä
SELECT word FROM dictionary WHERE word COLLATE NOCASE LIKE 'APPLE';
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º
LIKE Óï¾äʹÓÃģʽƥÅ䣬¶ø²»ÊÇÍêÈ«Æ¥Åä¡£
Æ¥Åä·û % ºÍ _ ±ØÐèתÒå²Å»ªÆ¥ÅäÆä×Ô¼º¡£
¹ØÓÚÐÔÄÜ×î¼Ñ»¯£¬ÇëÔÚ LIKE ģʽµÄ¿ªÍ·ºÍ×îºóʹÓà %¡£
↩
ÒÔÉϾÍÊÇsqlÖÐlikeÓï¾äÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡