PostgreSQL æä¾äºä¸ç§å®ç°æ¨¡å¼å¹é
çæ¹æ³ï¼ä¼ ç» SQL ç LIKE æä½ç¬¦ãSQL99 æ°å¢ç SIMILAR TO æä½ç¬¦ã POSIX é£æ ¼ç
æ£å表达å¼ãå¦å¤è¿æä¸ä¸ªæ¨¡å¼å¹é
å½æ° substringå¯ç¨ï¼å®å¯ä»¥ä½¿ç¨ SIMILAR TO é£æ ¼æè
POSIX é£æ ¼çæ£å表达å¼
LIKE
string LIKE pattern [ESCAPE escape-character]
string NOT LIKE pattern [ESCAPE escape-character]
æ¯ä¸ª pattern å®ä¹ä¸ä¸ª
å符串çéåãå¦æ该 string å
å«å¨ pattern 代表çå符串éåéï¼é£ä¹ LIKE 表达å¼è¿åçãåæ们æ³åçä¸æ ·ï¼å¦æ LIKE è¿åçï¼é£ä¹ NOT LIKE 表达å¼å°è¿ååï¼åä¹äº¦ç¶ãä¸ä¸ªçæç表达å¼æ¯ NOT (string LIKE pattern) ã
å¦æ pattern ä¸å
å«
ç¾åå·æè
ä¸å线ï¼é£ä¹è¯¥æ¨¡å¼åªä»£è¡¨å®æ¬èº«ï¼è¿æ¶å LIKE çè¡ä¸ºå°±åçå·æä½ç¬¦ãå¨ pattern éçä¸å线(_)å¹é
ä»»ä½å个å符ï¼èä¸ä¸ªç¾åå·(%)å¹é
é¶æå¤ä¸ªä»»ä½å符ã
ä¸äºä¾åï¼
'abc' LIKE 'abc' true
'abc' LIKE 'a%' true
'abc' LIKE '_b_' true
'abc' LIKE 'c' false
LIKE 模å¼å¹é
æ»æ¯è¦çæ´ä¸ªå符串ãè¦å¹é
å¨å符串å
é¨ä»»ä½ä½ç½®çåºåï¼è¯¥æ¨¡å¼å¿
须以ç¾åå·å¼å¤´åç»å°¾ã
è¦å¹é
ä¸å线æè
ç¾åå·æ¬èº«ï¼å¨ pattern éç¸åºçå符å¿
é¡»å导éé¸å符ã缺ççéé¸å符æ¯åææ ï¼ä½æ¯ä½ å¯ä»¥ç¨ ESCAPE åå¥æå®ä¸ä¸ªãè¦å¹é
éé¸å符æ¬èº«ï¼å两个éé¸å符ã
请注æåææ å¨å符串ææ¬éå·²ç»æç¹æ®å«ä¹äºï¼æ以å¦æä½ åä¸ä¸ªå
å«åææ ç模å¼
常éï¼é£ä½ å°±è¦å¨ SQL è¯å¥éå两个åææ ãå æ¤ï¼åä¸ä¸ªå¹é
å个åææ ç模å¼å®é
ä¸è¦å¨è¯å¥éåå个åææ ãä½ å¯ä»¥éè¿ç¨ ESCAPE éæ©ä¸ä¸ªä¸åçéé¸å符æ¥é¿å
è¿æ ·ï¼è¿æ ·åææ å°±ä¸åæ¯ LIKE ç
ç¹æ®å符äºãä½ä»ç¶æ¯å符ææ¬åæå¨çç¹æ®å符ï¼æä»¥ä½ è¿æ¯éè¦ä¸¤ä¸ªåææ ã
æ们ä¹å¯ä»¥éè¿åæ ESCAPE '' çæ¹å¼å
³ééé¸æºå¶ï¼è¿æ¶ï¼æ们就ä¸è½å
³éä¸å线åç¾åå·çç¹æ®å«ä¹ã
å
³é®å ILIKE å¯ä»¥ç¨äºæ¿æ¢ LIKE ï¼ä»¤è¯¥å¹é
å°±å½åçåºå设置æ¯å¤§å°åæ å
³çãè¿ä¸ªç¹æ§ä¸æ¯ SQL æ åï¼æ¯ PostgreSQL æ©å±ã
æä½ç¬¦ ~~ çæäº LIKE ï¼è ~~* çæäº ILIKE ãè¿æ !~~ å !~~* æä½ç¬¦åå«ä»£è¡¨ NOT LIKE å NOT ILIKE ãææè¿äºæä½ç¬¦é½æ¯ PostgreSQL ç¹æç.
ä¾å: æ¥æ¾æ°æ®åºä¸è¡¨çåå(æ¥æ¾ä»¥tbl_å¼å¤´ç表çåå)
select tablename from pg_tables where tablename like 'tbl!_%'ESCAPE '!';
å©ç¨escape转ä¹: ä¸é¢ç
sqlè¯å¥ä¸å°±æ¯å°!ä½ä¸º
转ä¹å符,ä½ç¨æ¯å°"_"转æ¢ææ®éç"_"