mysqlÕýÔò±í´ïʽ LIKE ͨÅä·û
ÓÉMySQLÌṩµÄģʽƥÅäµÄÆäËûÀàÐÍÊÇʹÓÃÀ©Õ¹ÕýÔò±í´ïʽ¡£µ±Äã¶ÔÕâÀàģʽ½øÐÐÆ¥Åä²âÊÔʱ£¬Ê¹ÓÃREGEXPºÍNOT REGEXP²Ù×÷·û(»òRLIKEºÍNOT RLIKE£¬ËüÃÇÊÇͬÒå´Ê)¡£ À©Õ¹ÕýÔò±í´ïʽµÄһЩ×Ö·ûÊÇ£º
“.”Æ¥ÅäÈκε¥¸öµÄ×Ö·û¡£
Ò»¸ö×Ö·ûÀà“[...]”Æ¥ÅäÔÚ·½À¨ºÅÄÚµÄÈκÎ×Ö·û¡£ÀýÈ磬“[abc]”Æ¥Åä“a”¡¢“b”»ò“c”¡£ÎªÁËÃüÃû×Ö·ûµÄÒ»¸ö·¶Î§£¬Ê¹ÓÃÒ»¸ö“-”¡£“[a-z]”Æ¥ÅäÈκÎСд×Öĸ£¬¶ø“[0-9]”Æ¥ÅäÈκÎÊý×Ö¡£
“ * ”Æ¥ÅäÁã¸ö»ò¶à¸öÔÚËüÇ°ÃæµÄ¶«Î÷¡£ÀýÈ磬“x*”Æ¥ÅäÈκÎÊýÁ¿µÄ“x”×Ö·û£¬“[0-9]*”Æ¥ÅäµÄÈκÎÊýÁ¿µÄÊý×Ö£¬¶ø“.*”Æ¥ÅäÈκÎÊýÁ¿µÄÈκζ«Î÷¡£
ÕýÔò±í´ïʽÊÇÇø·Ö´óСдµÄ£¬µ«ÊÇÈç¹ûÄãÏ£Íû£¬ÄãÄÜʹÓÃÒ»¸ö×Ö·ûÀàÆ¥ÅäÁ½ÖÖд·¨¡£ÀýÈ磬“[aA]”Æ¥ÅäСд»ò´óдµÄ“a”¶ø“[a-zA-Z]”Æ¥ÅäÁ½ÖÖд·¨µÄÈκÎ×Öĸ¡£
Èç¹ûËü³öÏÖÔÚ±»²âÊÔÖµµÄÈκεط½£¬Ä£Ê½¾ÍÆ¥Åä(Ö»ÒªËûÃÇÆ¥ÅäÕû¸öÖµ£¬SQLģʽƥÅä)¡£
ΪÁ˶¨Î»Ò»¸öģʽÒÔ±ãËü±ØÐëÆ¥Åä±»²âÊÔÖµµÄ¿ªÊ¼»ò½á⣬ÔÚģʽ¿ªÊ¼´¦Ê¹ÓÓ^”»òÔÚģʽµÄ½áβÓÓ$”¡£
ΪÁË˵Ã÷À©Õ¹ÕýÔò±í´ïʽÈçºÎ¹¤×÷£¬ÉÏÃæËùʾµÄLIKE²éѯÔÚÏÂÃæʹÓÃREGEXPÖØд£º
ΪÁËÕÒ³öÒÔ“b”¿ªÍ·µÄÃû×Ö£¬Ê¹ÓÓ^”Æ¥ÅäÃû×ֵĿªÊ¼²¢ÇÒ“[bB]”Æ¥ÅäСд»ò´óдµÄ“b”£º
mysql> SELECT * from pet WHERE name REGEXP "^[bB]";
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
ΪÁËÕÒ³öÒÔ“fy”½áβµÄÃû×Ö£¬Ê¹ÓÓ$”Æ¥ÅäÃû×ֵĽáβ£º
mysql> SELECT * from pet WHERE name REGEXP "fy$";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-
Ïà¹ØÎĵµ£º
Ò»Ö±ÒÔÀ´ÎÒµÄÎÄÕ¶¼ÊÇÒÔ¹¤×÷±Ê¼ÇµÄÐÎʽ´æÔÚ¡£Õâ¶ÔÓÚ×Ô¼º¶ÁÆðÀ´Ã»Ê²Ã´´ó°£¬µ«ÊǶÔÓÚÆäËû¶ÁÕß¿ÉÄܾÍÓÐЩÃÔã¡£ÎÒ¾¡Á¿¸Ä¸Ä£¬»»ÖÖ·½Ê½¡£
1. MySQL µÄ°æ±¾
Èç¹ûÒªÖ§³Ö·ÖÇø±í£¬ÔòÓ¦¸ÃʹÓà 5.1.x ÒÔÉϵİ汾¡£Èç
mysql> show variables like '%version%';
+-------------------------+------------------------------ ......
mysql> create table testdate(
-> id int not null auto_increment primary key,
-> time date);
Query OK, 0 rows affected (0.30 sec)
mysql> insert into testdate(time) values('2010-4-23');
Q ......
http://immike.net/blog/2007/04/09/how-not-to-optimize-a-mysql-query/
ÕâƪÎÄÕ½¨Ò飺
£¨1£©²»ÒªÊ¹ÓÃһЩmysqlµÄtip£¬È磺SQL_SMALL_RESULT, HIGH/LOW_PRIORITY, and INSERT DELAYED£¬STRAIGHT_JOIN£¬³ý·ÇûÓÐÆäËû·½·¨£¬ÒòΪÕâÑùʧȥÁËSQLµÄ¿ÉÒÆÖ²ÐÔ
£¨2£©Êʵ±¶Ô±í½øÐÐË÷Òý
£¨3£©±ÜÃâ¶Ô²»±ØÒªÊý¾ÝµÄ²éѯ
£¨4£©´¦Àí× ......
# MySQL-Front 5.1 (Build 4.2)
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
/*!40101 SET SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
/*!40103 SET SQL_NOTES='ON' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS */ ......