mysqlÖÐlikeÔõôÓÃ
like ÔËËã·ûÓÃÓÚ mysql ÖеÄģʽƥÅ䣬ËüʹÓÃͨÅä·ûÆ¥Åä×Ö·ûÐòÁУ¬°üÀ¨£º%£ºÆ¥ÅäÁã¸ö»ò¶à¸ö×Ö·û¡£_£ºÆ¥Åäµ¥¸ö×Ö·û¡£[ ]£ºÆ¥Åä·½À¨ºÅÄÚÁгöµÄÈκÎ×Ö·û¡£
MySQL ÖÐ LIKE ÔËËã·ûµÄʹÓÃ
LIKE ÔËËã·ûÓÃÓÚÔÚ MySQL ÖоÙÐÐģʽƥÅä¡£ËüÔÊÐíÄú²éÕÒÓëÌض¨Ä£Ê½Æ¥ÅäµÄÖµ£¬¸Ãģʽ¿ÉÒÔ°üÀ¨Í¨Åä·û¡£
ͨÅä·û£º
%£ºÆ¥ÅäÁã¸ö»ò¶à¸ö×Ö·û¡£
_£ºÆ¥Åäµ¥¸ö×Ö·û¡£
[list]£ºÆ¥Åä·½À¨ºÅÄÚÁгöµÄÈκÎ×Ö·û¡£
Óï·¨£º
SELECT column_name FROM table_name WHERE column_name LIKE 'pattern';
µÇ¼ºó¸´ÖÆ
ʾÀý£º
²éÕÒËùÓÐÒÔ “John” ¿ªÍ·µÄÃû×Ö£º
SELECT name FROM customers WHERE name LIKE 'John%';
µÇ¼ºó¸´ÖÆ
²éÕÒËùÓаüÀ¨×Öĸ “a” µÄÃû×Ö£º
SELECT name FROM customers WHERE name LIKE '%a%';
µÇ¼ºó¸´ÖÆ
²éÕÒËùÓÐÒÔ “John” ¿ªÍ·ÇÒÒÔ “son” ×îºóµÄÃû×Ö£º
SELECT name FROM customers WHERE name LIKE 'John%son';
µÇ¼ºó¸´ÖÆ
ÌáÐÑ£º
ͨÅä·û % ºÍ _ ¿ÉÒÔÔÚģʽÖеÄÈκÎλÖÃʹÓá£
¿ÉÒÔʹÓ÷½À¨ºÅ [ ] ½¨Éè×Ö·û¹æÄ£¡£ÀýÈ磬'[a-z]’ Æ¥ÅäÈκÎСд×Öĸ¡£
LIKE ÔËËã·ûÇø·Ö¾Þϸд£¬³ý·ÇʹÓà BINARY Òªº¦×Ö¡£ÀýÈ磬’John%’ Æ¥Åä “John”£¬µ« ‘john%’ ²»Æ¥Åä¡£
ÈôÊÇģʽÖаüÀ¨Í¨Åä·û£¬ÔòÓ¦½«Í¨Åä·ûתÒ壬ÒÔÃâËüÃDZ»Ú¹ÊÍΪÎı¾¡£ÀýÈ磬’\%’ Æ¥Åä “%” ×Ô¼º¡£
ÒÔÉϾÍÊÇmysqlÖÐlikeÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡