SQL LIKEµÄÓ÷¨
LIKE ÊÇÁíÒ»¸öÔÚ WHERE ×Ó¾äÖлáÓõ½µÄÖ¸Áî¡£»ù±¾ÉÏ£¬LIKE ÄÜÈÃÎÒÃÇÒÀ¾ÝÒ»¸öÌ×ʽ (pattern) À´ÕÒ³öÎÒÃÇÒªµÄ×ÊÁÏ¡£Ïà¶ÔÀ´Ëµ£¬ÔÚÔËÓà IN µÄʱºò£¬ÎÒÃÇÍêÈ«µØÖªµÀÎÒÃÇÐèÒªµÄÌõ¼þ£»ÔÚÔËÓà BETWEEN µÄʱºò£¬ÎÒÃÇÔòÊÇÁгöÒ»¸ö·¶Î§¡£ LIKE µÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "À¸Î»Ãû" LIKE {Ì×ʽ}
{Ì×ʽ} ¾³£°üÀ¨Ò°¿¨ (wildcard). ÒÔÏÂÊǼ¸¸öÀý×Ó£º
'A_Z': ËùÓÐÒÔ 'A' ÆðÍ·£¬ÁíÒ»¸öÈκÎÖµµÄ×ÖÔ£¬ÇÒÒÔ 'Z' Ϊ½áβµÄ×Ö´®¡£ 'ABZ' ºÍ 'A2Z' ¶¼·ûºÏÕâÒ»¸öģʽ£¬¶ø 'AKKZ' ²¢²»·ûºÏ (ÒòΪÔÚ A ºÍ Z Ö®¼äÓÐÁ½¸ö×ÖÔ£¬¶ø²»ÊÇÒ»¸ö×ÖÔ)¡£
'ABC%': ËùÓÐÒÔ 'ABC' ÆðÍ·µÄ×Ö´®¡£¾ÙÀýÀ´Ëµ£¬'ABCD' ºÍ 'ABCABC' ¶¼·ûºÏÕâ¸öÌ×ʽ¡£
'%XYZ': ËùÓÐÒÔ 'XYZ' ½áβµÄ×Ö´®¡£¾ÙÀýÀ´Ëµ£¬'WXYZ' ºÍ 'ZZXYZ' ¶¼·ûºÏÕâ¸öÌ×ʽ¡£
'%AN%': ËùÓк¬ÓÐ 'AN' Õâ¸öÌ×ʽµÄ×Ö´®¡£¾ÙÀýÀ´Ëµ£¬ 'LOS ANGELES' ºÍ 'SAN FRANCISCO' ¶¼·ûºÏÕâ¸öÌ×ʽ¡£
ÎÒÃǽ«ÒÔÉÏ×îºóÒ»¸öÀý×ÓÓÃÔÚÎÒÃÇµÄ Store_Information ±í¸ñÉÏ:
Store_Information ±í¸ñ
store_name
Sales
Date
LOS ANGELES
$1500
Jan-05-1999
SAN DIEGO
$250
Jan-07-1999
SAN FRANCISCO
$300
Jan-08-1999
BOSTON
$700
Jan-08-1999
ÎÒ們¾Í´òÈ룬
SELECT *
from Store_Information
WHERE store_name LIKE '%AN%'
結¹û:
store_name
Sales
Date
LOS ANGELES
$1500
Jan-05-1999
SAN FRANCISCO
$300
Jan-08-1999
SAN DIEGO
$250
Jan-07-1999
Ïà¹ØÎĵµ£º
ÓÃjdbcÁ¬½ÓSQL Server2005³öÏÖµ½Ö÷»ú µÄ TCP/IP Á¬½Óʧ°Ü¡£ java.net.ConnectException: Connection refused: connect!
¹À¼ÆÊÇÒòΪsqlserver2005ĬÈÏÇé¿öÏÂÊǽûÓÃÁËtcp/ipÁ¬½Ó¡£
Äú¿ÉÒÔÔÚÃüÁîÐÐÊäÈ룺telnet localhost 1433½øÐмì²é£¬Õâʱ»á±¨´í£ºÕýÔÚÁ¬½Óµ½localhost...²»ÄÜ´ò¿ªµ½Ö÷»úµÄÁ¬½Ó£¬ÔÚ¶Ë¿Ú 1433: Á¬½Óʧ°Ü
Æ ......
¼ÙÉèÈçÏÂÓ¦Óãº
Á½Õű헗Óû§±íTDefUser£¨userid£¬address,phone£©ºÍÏû·Ñ±íTAccConsume(userid,time,amount)£¬ÐèÒª²éÏû·Ñ³¬¹ý5000µÄÓû§¼Ç¼¡£
ÓÃexists:
select * from TDefUser
where exists (select 1 from TAccConsume where TDefUser.userid=TAccConsume.userid and TAccConsume.amount>5000)
ÓÃi ......
ÔÚÎÒÃÇƽʱд³ÌÐòµÄʱºò£¬ÓÐЩ²ÎÊýÊǾ³£¸Ä±äµÄ£¬¶øÕâÖָı䲻ÊÇÎÒÃÇÔ¤ÖªµÄ¡£±ÈÈç˵ÎÒÃÇ¿ª·¢ÁËÒ»¸ö²Ù×÷Êý¾Ý¿âµÄÄ£¿é£¬ÔÚ¿ª·¢µÄʱºòÎÒÃÇÁ¬½Ó±¾µØµÄÊý¾Ý¿âÄÇô IP £¬Êý¾Ý¿âÃû³Æ£¬±íÃû³Æ£¬Êý¾Ý¿âÖ÷»úµÈÐÅÏ¢ÊÇÎÒÃDZ¾µØµÄ£¬ÒªÊ¹µÃÕâ¸ö²Ù×÷Êý¾ÝµÄÄ£¿é¾ßÓÐͨÓÃÐÔ£¬ÄÇôÒÔÉÏÐÅÏ¢¾Í²»ÄÜдËÀÔÚ³ÌÐòÀͨ³£ÎÒÃǵÄ×ö·¨ÊÇÓÃÅäÖÃÎļþÀ´½â ......
SELECT Ö¸ÁîÈÃÎÒÃÇÄܹ»¶ÁÈ¡±í¸ñÖÐÒ»¸ö»òÊý¸öÀ¸Î»µÄËùÓÐ×ÊÁÏ¡£ Õ⽫°ÑËùÓеÄ×ÊÁ϶¼×¥³ö£¬ÎÞÂÛ×ÊÁÏÖµÓÐÎÞÖظ´¡£ÔÚ×ÊÁÏ´¦ÀíÖУ¬ÎÒÃǻᾳ£Åöµ½ÐèÒªÕÒ³ö±í¸ñÄڵIJ»Í¬ ×ÊÁÏÖµµÄÇé¿ö¡£»»¾ä»°Ëµ£¬ÎÒÃÇÐèÒªÖªµÀÕâ¸ö±í¸ñ/À¸Î»ÄÚÓÐÄÄЩ²»Í¬µÄÖµ£¬¶øÿ¸öÖµ³öÏֵĴÎÊý²¢²»ÖØÒª¡£ÕâÒªÈçºÎ´ï³ÉÄØ£¿ÔÚ SQL ÖУ¬ÕâÊǺÜÈÝÒ××öµ½µÄ¡£ÎÒÃÇÖ»ÒªÔ ......
IN Õâ¸öÖ¸Áî¿ÉÒÔÈÃÎÒÃÇÒÀÕÕÒ»»òÊý¸ö²»Á¬Ðø (discrete) µÄÖµµÄÏÞÖÆÖ®ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ£¬¶ø BETWEEN ÔòÊÇÈÃÎÒÃÇ¿ÉÒÔÔËÓÃÒ»¸ö·¶Î§ (range) ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ¡£BETWEENÕâ¸ö×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from " ±í¸ñÃû"
WHERE "À¸Î»Ãû" BETWEEN 'ÖµÒ»' AND 'Öµ¶þ'
Õ⽫ѡ³öÀ¸Î»Öµ°üº¬ÔÚÖµÒ»¼°Öµ¶þÖ®¼äµÄÿһ±Ê× ......