sqlÖеÄlike
Ö´ÐÐ Êý¾Ý¿â²éѯʱ£¬ÓÐÍêÕû²éѯºÍÄ£ºý²éѯ֮·Ö¡£
Ò»°ãÄ£ºýÓï¾äÈçÏ£º
SELECT ×ֶΠfrom ±í WHERE ij×ֶΠLike Ìõ¼þ
ÆäÖйØÓÚÌõ¼þ£¬SQLÌṩÁËËÄÖÖÆ¥Åäģʽ£º
1£¬%£º±íʾÈÎÒâ0¸ö»ò¶à¸ö×Ö·û¡£¿ÉÆ¥ÅäÈÎÒâÀàÐͺͳ¤¶ÈµÄ×Ö·û£¬ÓÐЩÇé¿öÏÂÈôÊÇÖÐÎÄ£¬ÇëÔËÓÃÁ½¸ö°Ù·ÖºÅ£¨%%£©±íʾ¡£
±ÈÈç SELECT * from [user] WHERE u_name LIKE '%Èý%'
½«»á°Ñu_nameΪ“ÕÅÈý”£¬“ÕÅèÈý”¡¢“Èý½Å蔣¬“ÌÆÈý²Ø”µÈµÈÓГÈý”µÄ¼Ç¼ȫÕÒ³öÀ´¡£
ÁíÍ⣬Èç¹ûÐëÒªÕÒ³öu_nameÖмÈÓГÈý”ÓÖÓГ蔵ļǼ£¬ÇëÔËÓà andÌõ¼þ
SELECT * from [user] WHERE u_name LIKE '%Èý%' AND u_name LIKE '%è%'
ÈôÔËÓà SELECT * from [user] WHERE u_name LIKE '%Èý%è%'
ËäÈ»ÄÜËÑË÷³ö“Èý½Å蔣¬µ«²»ÄÜËÑË÷³ö·ûºÏÌõ¼þµÄ“ÕÅèÈý”¡£
2£¬_£º ±íʾÈÎÒâµ¥¸ö×Ö·û¡£Æ¥Åäµ¥¸öÈÎÒâ×Ö·û£¬Ëü³£ÓÃÀ´ÏÞ¶¨±í´ïʽµÄ×Ö·û³¤¶ÈÓï¾ä£º
±ÈÈç SELECT * from [user] WHERE u_name LIKE '_Èý_'
Ö»ÕÒ³ö“ÌÆÈý²Ø”ÕâÑùu_nameΪÈý¸ö×ÖÇÒÖмäÒ»¸ö×ÖÊÇ“Èý”µÄ£»
ÔÙ±ÈÈç SELECT * from [user] WHERE u_name LIKE 'Èý__';
Ö»ÕÒ³ö“Èý½Åè”ÕâÑùnameΪÈý¸ö×ÖÇÒµÚÒ»¸ö×ÖÊÇ“Èý”µÄ£»
3£¬[ ]£º±íʾÀ¨ºÅÄÚËùÁÐ×Ö·ûÖеÄÒ»¸ö£¨ÀàËÆÕýÔò±í´ïʽ£©¡£Ö¸¶¨Ò»¸ö×Ö·û¡¢×Ö·û´®»ò·¶Î§£¬ÒªÇóËùÆ¥Åä¶ÔÏóΪËüÃÇÖеÄÈÎÒ»¸ö¡£
±ÈÈç SELECT * from [user] WHERE u_name LIKE '[ÕÅÀîÍõ]Èý'
½«ÕÒ³ö“ÕÅÈý”¡¢“ÀîÈý”¡¢“ÍõÈý”£¨¶ø²»ÊÇ“ÕÅÀîÍõÈý”£©£»
Èç [ ] ÄÚÓÐһϵÁÐ×Ö·û£¨01234¡¢abcdeÖ®ÀàµÄ£©Ôò¿ÉÂÔдΪ“0-4”¡¢“a-e”
SELECT * from [user] WHERE u_name LIKE 'ÀÏ[1-9]'
½«ÕÒ³ö“ÀÏ1”¡¢“ÀÏ2”¡¢……¡¢“ÀÏ9”£»
4£¬[^ ] £º±íʾ²»ÔÚÀ¨ºÅËùÁÐÖ®Äڵĵ¥¸ö×Ö·û¡£ÆäÈ¡ÖµºÍ [] Ïàͬ£¬µ«ËüÒªÇóËùÆ¥Åä¶ÔÏóΪָ¶¨×Ö·ûÒÔÍâµÄÈÎÒ»¸ö×Ö·û¡£
±ÈÈç SELECT * from [user] WHERE u_name LIKE '[^ÕÅÀîÍõ]Èý'
½«ÕÒ³ö²»ÐÕ“ÕÅ”¡¢“À¢“Íõ”µÄ“ÕÔÈý”¡¢“ËïÈý”µÈ£»
SELECT * from [user] WHERE u_name LIKE 'ÀÏ[^1-4]';
½«Åųý“ÀÏ1”µ½“ÀÏ4”£¬Ñ°ÕÒ“ÀÏ5&rd
Ïà¹ØÎĵµ£º
SQL Server 2000ÖУ¬ÓÐÈý¸ö±È½ÏÀàËƵŦÄÜ£ºËûÃÇ·Ö±ðÊÇ£ºSCOPE_IDENTITY¡¢IDENT_CURRENT ºÍ @@IDENTITY£¬ËüÃǶ¼·µ»Ø²åÈëµ½ IDENTITY ÁÐÖеÄÖµ¡£
IDENT_CURRENT ·µ»ØΪÈκλỰºÍÈκÎ×÷ÓÃÓòÖеÄÌض¨±í×îºóÉú³ÉµÄ±êʶֵ¡£IDENT_CURRENT ²»ÊÜ×÷ÓÃÓòºÍ»á»°µÄÏÞÖÆ£¬¶øÊÜÏÞÓÚÖ¸¶¨µÄ±í¡£IDENT_CURRENT ·µ»ØΪÈκλỰºÍ×÷ÓÃÓòÖÐµÄ ......
Ò»°ãµÄ¹«Ë¾Í¨³£»áÔÚËûÃǵÄÐÅϢϵͳ¼Ü¹¹ÖÐÒýÈë¶àÖÖÊý¾Ý¿âƽ̨£¬Í¬Ê±ÒýÈëÈýµ½ËÄÖÖ²»Í¬µÄRDBMS½â¾ö·½°¸µÄÖдóÐ͹«Ë¾Ò²²¢²»ÉÙ¼û£¬µ±È»ÕâЩ¹«Ë¾ÀïÃæµÄDBAÃÇͨ³£Ò²ÐèҪͬʱӵÓйÜÀí¶àÖÖ²»Í¬Æ½Ì¨µÄ¼¼ÄÜÁË¡£
Ö»ÔÚÒ»ÖÖƽ̨ÉÏÕ¹¿ª¹¤×÷µÄÊý¾Ý¿âר¼ÒÃÇҲͨ³£»áÆÚ´ý×ÅÔÚËûÃǵÄÏÂÒ»·Ý¹¤×÷ÖÐÄÜѧµ½µã²»Ò»ÑùµÄ¶«Î÷£¬ÄÇЩÓÐÓÂÆøµÄÈËÃÇÔòÔ¸Òâ ......
Ò»£®ÊÓͼ(VIEW)
¶¨Ò壺ÊÓͼÊÇÒ»ÕÅÐéÄâµÄ±í£¬ÆäÄÚÈÝÊÇ´ÓÒ»ÕÅ»ò¶àÕűíÖвéѯµÃÀ´µÄ½á¹û¼¯¡£
ÃèÊö£ºÊÓͼÊÇ´æ´¢ÔÚϵͳĿ¼ÖеÄÐÅÏ¢£¬Ëý²¢Ã»ÓÐÕæÕý´æ´¢Êý¾Ý£¬¶øÊÇÒÑ´æ±íµÄͶӰ¡£ÊÓͼµÄÊôÐÔÊÇ°´ÕÕÒ»¸ö²éѯµÄ¹æ¶¨´ÓÒ»¸ö»òÕ߶à¸ö±íÖе¼³öµÄ£¬ÊÇÒÑ´æ±íµÄ¹ýÂËÆ÷¡£ËýÊÇÒ»ÕÅ“Ðé±í”£¬´ÓÒ»Õűí»òÕ߶àÕűíÖÐÅÉÉú³öÀ´µÄ£¬Æä ......
СµÜÏëÎʸöÎÊÌ⣬ÔÚSQL Server 2005ÉÏ£¬½¨Á¢Ö÷¼üµÄͬʱ¾Í»áĬÈÏÔÚÖ÷¼üÉÏÉèÖþ۴ØË÷Òý£¬ÄÇôÄÜ·ñÔÚÒ»¸ö×Ö¶ÎÉÏÉèÖÃÖ÷¼üºÍΨһÐÔË÷Òý£¨Unique£©£¬µ«ÊǰѾ۴ØË÷ÒýÉèÖõ½ÁíÒ»¸ö×Ö¶ÎÉÏ£¿
¾Ù¸ö¼òµ¥µÄÀý×Ó£¬±ÈÈçÎÒÓиö±í½ÐTableTest£¬±íÀïÓÐÁ½¸ö×ֶΣ¬id,date£¬±¾À´idÊÇÖ÷¼üµÄ£¬ÎÒÏÖÔÚÏë°Ñ¾Û´ØË÷ÒýÉèÖõ½date×Ö¶ÎÉÏ¡£
ÎÒÏÈɾ³ýÖ ......