ä¾ãã°ãã\%fooãããå§ã¾ãæååãæ¤ç´¢ããå ´åã«ã¯ãã©ã®ãããªSQLæãæ¸ãã°ããã®ã§ããããã
æ¡ä»¶ã¯ä»¥ä¸ã®éãã§ãã
MySQLã§ESCAPEç¯ã使ããªãå ´åãã¯ã¤ã«ãã«ã¼ãæåï¼ã%ããã_ãï¼ã¯ã\ãã§ã¨ã¹ã±ã¼ããããã¨ã«ãªãã¾ãã
ééã£ãçã
ç´æçã«ä»¥ä¸ã®ãããªSQLæãæ¸ãã¦ãã¾ã人ãããã¨æãã¾ãã
SELECT * FROM table1 WHERE hoge LIKE '\\\%foo%';
å®éã«è©¦ãã¦è¦ã¾ãã
mysql> SELECT 123 FROM dual WHERE '\\%foo456' LIKE '\\\%foo%'; +-----+ | 123 | +-----+ | 123 | +-----+ 1 row in set (0.00 sec) mysql> SELECT 123 FROM dual WHERE '%foo456' LIKE '\\\%foo%'; Empty set (0.01 sec)
æåã®SQLæã§ã¯ãã\%foo456ãã¨ããæååã調ã¹ã¦ãã¾ããã¬ã³ã¼ããè¿ã£ã¦ãã¦ããã®ã§ãLIKEæ¼ç®åã®çµæã¯çã§ãããã®æ¬¡ã®SQLæã§ã¯ãã%foo456ãã«ã¤ãã¦èª¿ã¹ã¦ãã¾ããLIKEæ¼ç®åã¯å½ã®å¤ãè¿ãã¦ãããã¬ã³ã¼ãã¯è¿ããã¾ããã
ããã ããè¦ãã¨ãã¾ããã£ã¦ãã£ã¦ããããã«æãã¾ãããå®éã¯ããã§ã¯ããã¾ããã次ã®SQLã®çµæãè¦ãã¨ããããå¤ãã¾ãã
mysql> SELECT 123 FROM dual WHERE '\\xxxfoo456' LIKE '\\\%foo%'; +-----+ | 123 | +-----+ | 123 | +-----+ 1 row in set (0.00 sec)
ã'\\xxxfoo456' LIKE '\\\%foo%'
ãã®çµæã¯çã¨ãªã£ã¦ãã¾ãããªããããªããã¨ããã¨ã%ã®ã¨ã¹ã±ã¼ãã«å¤±æãã¦ããããxxxãã®é¨åã%ï¼ã¯ã¤ã«ãã«ã¼ãï¼ã§ããããã¦ãã¾ã£ã¦ããããã§ãã
æ£è§£
ã\%fooãããå§ã¾ãæååãæ¤ç´¢ããSQLæã¯ã以ä¸ã®ããã«ãªãã¾ãã
mysql> SELECT 123 FROM dual WHERE '\\%foo456' LIKE '\\\\\\%foo%'; +-----+ | 123 | +-----+ | 123 | +-----+ 1 row in set (0.00 sec) mysql> SELECT 123 FROM dual WHERE '\\xxxfoo456' LIKE '\\\\\\%foo%'; Empty set (0.00 sec)
ã¡ãã£ã¨ã\ããå¤ãããããªãã®ï¼ã¨æãããããããã¾ããããæå¾ éãã\%foo456ãã¯çã¨ãªããã\xxxfoo456ãã¯å½ã¨ãªã£ã¦ãã¾ãã
å¦çã®æ¹æ³
LIKEã«ä¸ããæååã®ã¨ã¹ã±ã¼ãå¦çã¯ã以ä¸ã®ããã«è¡ãªãã°ããã¨æãã¾ãã
- LIKEæ¼ç®åã®ã¯ã¤ã«ãã«ã¼ãæåã®ã¨ã¹ã±ã¼ã
ã%ãã_ãã\ããã\ãã§ã¨ã¹ã±ã¼ããã¾ãï¼å½ç¶ãã¯ã¤ã«ãã«ã¼ãã©ãã¦æ©è½ããããã%ãã_ãã¯ã¨ã¹ã±ã¼ãããªãï¼ã - SQLæååã®ã¨ã¹ã±ã¼ã
ã\ãã'ãã"ãã¨NULLãæ¹è¡æåãªã©ãã\ãã§ã¨ã¹ã±ã¼ããã¾ãã
ãã®2ã¤ã®å¦çãã1ã2ã®é çªã§ããã¾ãï¼DBMSå´ã§ã®SQLæã®è§£ææã«ã¯ã2â1ã¨ããé ã§ãã³ã¼ãå¦çãããã¯ãã§ãï¼ã
1ã®å¦çã®æ¹ã¯ããã°ã©ã ãèªä½ããå¿ è¦ãããã¨æãã¾ããä¸æ¹2ã¯ããã¤ã³ãæ©æ§ï¼Prepared Statementï¼ã使ããç°å¢ãªãã°ãã¤ã³ãã使ããããã§ãªããã°MySQLã®ã¨ã¹ã±ã¼ãAPIï¼ãã©ãããããã®ï¼ã使ãã ãã§ãã
1ã¨2ãåããã«ãã£ã¡ãã«ãã¦ãããã1ã§ã\ããã¨ã¹ã±ã¼ããã¦ããªãã£ãããé çªãéã«ãªã£ã¦ãããã¨ãããããªééãã¯ããããããããªæ°ããã¾ãã