INã¨EXISTSã®éã
ãINã¨EXISTSã¯éãã¾ãã
ãBETWEENã¨ãä¸çå·ã®çµåãããªã©ãç価ã«ãªãè¨è¿°æ³ã¯ããã®ã§ãããã©ãINã¨EXISTSã¯åºæ¬çã«åãçµæãè¿ããã¨ãå¯è½ã§ãããæå³ã¯éãã¾ãã
ããã®éããåããã«ã¯ã¤ã³ããã¯ã¹ãç解ããå¿ è¦ãããã¾ãã®ã§ãã¾ãã¯ãインデックスのイメージãã¤ãã¦ãã ããã
ã¾ãã¯ã¤ã¡ã¼ã¸
ãããã§ããã¾ãã¯ã¤ã¡ã¼ã¸ã§èãã¾ããããã
ãããªãã¯å 輩ã®çµå©å¼ã®å¸ä¼ãé ¼ã¾ãã¾ããã¨ãã¾ããã¤ãã¤ãã¨æºåãããã¾ãããä½èã§æãæã人ãããã¨ããäºãã«ã©ãªã±ã®çªå·ã調ã¹ã¦ããã§ãããã
ãé£äºã®éã®BGMã«ã¤ãã¦ã¯ãã©ãã½ã³ã°ã®å ¥ã£ãiPodãã¤ãªãã§ã©ã³ãã ã§æµããã¨ã«ãã¾ããããããããæ°éæ°å©¦ã«ã¨ã£ã¦ï¼éå»ã®ææçµé¨ä¸ï¼é½åã®æªãæ²ãããããã§ãã¯ãã¦ã¯ããã¦ãããã¨ã«ãã¾ããï¼ãªããªãããã¤ããªãå¸ä¼ã§ããªï¼ã
ããããã®å¦çãSQLã«ãããªãã°â¦â¦ã
â ã«ã©ãªã±ã®çªå·ãæºåããã®ãINã®å¦çã
ãããããSELECT *
ãããããFROM ã«ã©ãªã± â¦â¦
ãããããWHERE
ãããããããã«ã©ãªã±çªå· IN
ããããããããã(SELECT ã«ã©ãªã±çªå·
ãããããããããFROM æããªã¹ã
ããããããããã)
â BGMãiPodããæµãæ²ãæºåããã®ãEXISTSã®å¦çã«ãªãã¾ãã
ãããããSELECT *
ãããããFROM iPod
ãããããWHERE
ãããããããNOT EXISTS
ããããããããã(SELECT *
ãããããããããFROM æ°éæ°å©¦NGãªã¹ã
ãããããããããWHERE æ°éæ°å©¦NGãªã¹ã.æ²å = iPod.æ²å
ããããããããã)
ã⻠赤åã®é¨åã«ã¤ã³ããã¯ã¹ãããã°ãªããã£ãã¤ã¶ã¯ã¤ã³ããã¯ã¹ã使ãã¾ãã
ä½ãåºæºã«æ±ºããã°ããã
ã人éãé¢åãªãã¨ã¯ãã³ã³ãã¥ã¼ã¿ããã£ã¦ããã£ã±ãé¢åï¼ã³ã¹ããæããï¼ã®ã§ããææ³ãããªããã©ã¡ããã«æ±ºãæã¡ã§ããªããèªåãæä½æ¥ã§ããã¨ãããã©ã¡ããé¸ã¶ãèããã°èªãã¨çãã¯åºã¦ãã¾ãã
ãINã使ãã¨ããã¤ã¾ãã«ã©ãªã±ã®çªå·ãæºåããã¨ãã¯ãæããªã¹ããã¡ã¤ã³ã«ã¼ãã«é¸ãã§ãããã¨ãåããã§ããããï¼éã«ãEXISTSã使ãã¨ãã¯ãiPodãã¡ã¤ã³ã«ã¼ãã«é¸ãã§ãã¾ãã
ãã¤ã¾ãã
â INã®å ´å
ãããããforeach (int çªå· in æããªã¹ã)
ããããã{
ãããããããçµæã»ãã.add(ã«ã©ãªã±.find(çªå·));
ããããã}
â EXISTS
ãããããforeach (string æ²å in iPod)
ããããã{
ãããããããif(Arrays.binarySearch(æ°éæ°å©¦NGãªã¹ã, æ²å) < 0)
ããããããã{
ãããããããããçµæã»ãã.add(æ²å);
ããããããã}
ããããã}
ãã¨èãã¦ãè¯ãã
ãã¡ã¤ã³ã«ã¼ãã¨çµæã»ããã«å ¥ããã®ãåãã¨ãï¼èªã¿é£ã°ãï¼ã®ã¨ãã¯EXISTSãã¡ã¤ã³ã«ã¼ãã¨çµæã»ããã«å ¥ãããã®ãéãã¨ãã¯INã«ãªãã¾ãã
ãå¦çããèããã¨ããSQLã¯ä»æ§æ¸ã§ãããã¨ããæå³ãåããã¨æãã¾ãã
ãæç¶ãåè¨èªã§ã¡ã¤ã³ã«ã¼ããéã«ãã¦ãåãçããè¿ã£ã¦ããã®ã¨åãã§ãINã§ãEXISTSã§ãç価ã®çããè¿ããã¨ãã§ãã¾ããããããæ°äººãªãã¨ããããããã©ã³ããããªä»æ§æ¸ã»ããã°ã©ã ãæ¸ãã®ã¯ç¸å½ã«æ¥ãããããã¨ã§ãããã
ãã¨ããããSQLã§ã¯ã³ã¼ãã£ã³ã°è¦ç´ããããINã§æ¸ãã¦é ãã¨ãã«ã¯ãEXISTSã«ãã¦ã¿ããã¨ããããã¨ãã¦ããªãæ¥ããããå 容ãæ¸ãã¦ãããã¨ã¯çãããªãã§ããæå³ãåããã¨ããã£ã¦ã¯ãªããªããã¨ã ã¨åããã¨æãã¾ããâ¦â¦ã
ããä¸ã¤ã®é¸ã³æ¹
ãå¦çããèãã¦ãè¯ãã®ã§ãããã©ãããä¸ã¤ã®é¸ã³æ¹ãããã¾ãã使ãããã¤ã³ããã¯ã¹ãããå ´åã¯ã
ã
â INã®ã¨ãã¯
ãããããã«ã©ãªã±.ã«ã©ãªã±çªå·
â EXISTSã®ã¨ãã¯ã
ãããããæ°éæ°å©¦NGãªã¹ã.æ²å
ãã®ã¤ã³ããã¯ã¹ã使ç¨å¯è½ã§ãã®ã§ããããæãããã«æ±ºå®ãã¦ããã¾ãã¾ããã
ãæç¶ãåã®å¦çãè¦ãã°åããã¾ãããforeachã¯è¦ç´ ããªããªãã¾ã§ï¼breakããã¾ã§ï¼é ã«ã¢ã¯ã»ã¹ãã¾ãã®ã§ãã¡ã¤ã³ã«ã¼ãã®æ¹ã¯ã¤ã³ããã¯ã¹ãããããããªãããããé¢ä¿ã¯ãªãã§ãï¼ãã¡ãããä»ã®WHEREæ¡ä»¶ãããã¨ãã¯ã¡ã¤ã³ã«ã¼ãã®ã¤ã³ããã¯ã¹ã使ããã¨ã¯ããå¾ã¾ãï¼ã
ãã¨ã«ãããINã¨EXISTSã¯ç価ã®çµæãè¿ããã¨ãå¯è½ã§ããæå³ã¯éãã¾ããå¦çããèãã¦ãè¯ãããã©ã¡ãã®ã¤ã³ããã¯ã¹ã使ããããã§èãã¦ãè¯ãããã©ãæå³ãèããªãã§æ±ºãæã¡ã¯ããã¾ããããæå³ãèããªãã§æ¸ãããé·ãSQLã¯ãä½ããããã®ãåãããªãããå ¨ãèªããªããã®ã«ãªã£ã¦ãã¾ããSQLã¯éæ³ã§ãåªæã§ããªãã®ã§ãå¿ ããæå³ãèãã¦æ¸ãã¦ãã ããã
ä½è«ã§ã
ãç§ã®ç¥ãéããSQLServerï¼Accessãï¼ä»¥å¤ã®RDBMSã§ã¯ã
ãããããWHERE
ããããããã(ã«ã©ã 1, ã«ã©ã 2) IN
ããããããããã(SELECT ã«ã©ã A, ã«ã©ã B
ãããããããããFROM ãµããã¼ãã«
ãããããããããWHERE æ¡ä»¶
ããããããããã)
ãã¨ãããè¡å¤æ§æåãã¨ããè¡å¤å¼ãã¨ãããSQL99ããã®æ¸ãæ¹ãå¯è½ã§ãããSQLServerã§ã¯ãã®æ¸ãæ¹ãã§ãã¾ããããã®ãããè¤æ°ã®ã«ã©ã ãæ¡ä»¶ã¨ãã¦å¿ è¦ãªã¨ãã«ã¯ãSQLServerã§ã¯EXISTSã§æ¸ãããããã¾ããã
ãSQLServerã®ãã®ä»æ§ã¯ãã©ãã«ããªããªãããªã¨â¦â¦ããªãã¨ããã¦æ¬²ããã¨ããã§ããã