MVCããªãééã£ã¦ãããï¼
Webã·ã¹ãã ã«MVCãé©ç¨ããã®ã¯ééã£ã¦ãã¾ãï¼æ£ç¢ºã«ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã足ãã¾ããï¼ã
ãã®ããã«æèåæ¢ããã¨ã³ã¸ãã¢ã«ã¯ããããªãããããã¾ããããããä¸åº¦è§£èª¬ãã¦ã¿ã¾ãããã
ç®æ¬¡
- ç®æ¬¡
- ããããMVCã¨ã¯ï¼
- æ´å²ãæ¯ãè¿ã£ã¦ã¿ã¾ããã
- ã¤ã³ãã¼ãã³ã¹ãã¹ãããã¨ORMã®ç»å ´ï¼
- ããããã¹ãã§ãããï¼
- ã¹ãã¢ãããã·ã¼ã¸ã£ã«ãããã¨ã«åé¡ã¯ãªãã®ï¼
- Fetch ãã¦ã°ã«ã°ã«ããªã
- æååé£çµã¯è¦ããªã
- ã©ã¡ãã極端ã§ããï¼
- åé¢éçºããã¹ã
ããããMVCã¨ã¯ï¼
MVCã¯ãã¹ã¿ã³ãã¢ãã¼åãã®ããã°ã©ã ããã¾ãã«ãã¡ããã¡ãã ã£ãã®ã§ããéãæ©è½ã¯ççµåã«ãããï¼ãã¨ããèãã§ä½ãåºããã¾ããã 1980年代ã®é ã®ã話ã§ãã ããèªä½ã¯æ£ããã§ãã
æ´å²ãæ¯ãè¿ã£ã¦ã¿ã¾ããã
Webã·ã¹ãã ããMVCã«é©ç¨ãããããã¨è¨ããåºããã®ã¯2000å¹´é ããã§ãã
ãã®è¾ºããç解ããã«ã¯ãå°ãæ´å²ãç¥ãå¿ è¦ãããã¾ãã
1995å¹´é ï¼ç§ã®æ°äººæ代ï¼ã«Javaãçã¾ãã¾ããããå½æã¯JavaãWebã§ä½¿ãã®ã¯Appletã§ããï¼ã¯ã£ããè¨ã£ã¦ä½¿ãç©ã«ãªãã¾ããã§ããï¼ã 1995ï½2000å¹´ã«ããã¦ãY2Kï¼2000å¹´åé¡ï¼ãèµ·ãã¾ããã COBOLã§ã¯ã西æ¦ã2æ¡ã§ä¿åãããã¨ãä¸è¬çã ã£ããããã2000年以éã·ã¹ãã ãåããªããªããã¨ããåé¡ã§ãã·ã¹ãã ãæ¹ä¿®ããããã¯ã©ãµãã§ä½ãç´ããããããããé¸æãããã¯ã©ãµãã·ã¹ãã ãããããä½ããããã¨ã«ãªãã¾ããã ãã®ã¨ãRDBMSãã¡ã¸ã£ã¼ã«ãªãã人æã足ãã¾ããããCOBOLerã大éã«æµå ¥ãã¦ãã¾ããã
ã¤ãã³ãããªãã³ãåãããªãCOBOLerããä»æ§ã決ããããã³ã¼ãã¬ãã¥ã¼ããããããªä¸ã®ç«å ´ã«ããããã§ãã æ¬å½ã«æ··æ²ã¨ããæ代ã§ããã
ãã®ã¨ãCOBOLeréã¯ããRDBMSã¯ã¹ãã¬ã¼ã¸ãSQLã¯ãã¡ã¤ã«ã®èªã¿æ¸ããããæ¹è¨ãã¨ããéããã¾ããã
ã¤ãã³ãããªãã³ãåãããªãä¸ã«ãã俺ãåãããããé åã¯ç¦æ¢ãã¨ãè¨ãåºã人ããããããã¾ãããããç§ãã¡åãã£ã¦ããä¸ä»£ã¯ãã¾ãã¯SQL以å¤ã®ä¸æ¯ãªããã«ãç¹°ãåºããå¿ è¦ããããSQLã¾ã§æãåããªãã£ãã®ã§ãã
çµæããRDBMSã¯ã¹ãã¬ã¼ã¸ãSQLã¯ãã¡ã¤ã«ã®èªã¿æ¸ããããæ¹è¨ãã¨ããã®ãæåã¨ãã¦æ®ã£ã¦ãã¾ã£ãã®ã§ãã
2000年以åã¯ããã©ã¦ã¶ãCSSã«å¯¾å¿ãã¦ãã¾ããã§ããã HTMLèªä½ããã¶ã¤ã³ãåé¢ã§ãã¦ãã¾ããã§ãããããWebã·ã¹ãã ã§MVCãå±ãããã¨ãäºå®ä¸ç¡çã§ããã
2000å¹´ã«ãªããããã³ã ããã«ã®ä¸ã§ãServletãJSPãCSSï¼ã使ãããã©ã¦ã¶ï¼ãªã©ãçã¾ãã¾ãã
Viewãåé¢ãããã¨ãã§ããããã«ãªã£ã¦ããMVCã«ãããããã¨ããæµããã§ããã®ã§ãã
ãã®æµãèªä½ã¯ééãã§ã¯ããã¾ããããæ®å¿µãªããããRDBMSã¯ã¹ãã¬ã¼ã¸ãSQLã¯ãã¡ã¤ã«ã®èªã¿æ¸ããããæ¹è¨ãã¨ããCOBOLerã®éºä¼åã¯æ®ã£ã¦ãã¾ãã¾ããã
ã¤ã³ãã¼ãã³ã¹ãã¹ãããã¨ORMã®ç»å ´ï¼
MVCãé©ç¨ãã¦ããä¸ã§ããã¤ã³ãã¼ãã³ã¹ãã¹ããããã¨ãããã¨ã表é¢åããã¦ããã¾ãã
ããã解決ããããã«ãORMãªããã®ãéçºããã¾ããã
ãã¦ããã®å³ããã£ããè¦ã¦ã¿ã¾ããããViewã«ã¯ãCSSãJavaScriptãªã©ãWebãµã¼ãï¼ãã©ã¦ã¶ï¼ã管è½ãããã®ã¨ãAPãµã¼ãã管è½ãããã®ãããã¾ãã
Controlã¯ãAPãµã¼ãã«éå®ããã¾ãã
Modelã¯ãAPãµã¼ãã¨ãDBãµã¼ãã«åããã¾ããããªããSQLã¯APãµã¼ãã®ç®¡è½ã«ãªã£ã¦ãã¾ãã PHPï¼Eloquentï¼Rubyï¼ActiveRecordï¼ãªã©ã§è¦ã£ã¦ããç§ããè¦ãã°ããªãã¸ã§ã¯ãæåè¨èªçã«ã¯ãç°ç©ã以å¤ã«è¡¨ç¾ããããããã¾ããã ORMã使ã£ãã½ã¼ã¹ã¯ããªãã¸ã§ã¯ãæåè¨èªçã«ãã®ãããæ±ãã½ã¼ã¹ã«ããè¦ããªãã®ã§ããâ¦â¦ã ããã«ãORMã®æ©è½ãViewã«å½ã¦ã¯ãã¦èããã°ããPHPã§æ¸ãã°JavaScriptãCSSãèªåçæãã¦ãããããããå¥å¦ãªæ§é ã«ãªã£ã¦ãã¾ãã
ãããå¥å¦ã¨æããªã人ã¯ããRDBMSã¯ã¹ãã¬ã¼ã¸ãSQLã¯ãã¡ã¤ã«ã®èªã¿æ¸ããããæ¹è¨ãã¨ããCOBOLerãä½ã£ãæåãå¼ãç¶ãã§ããããã§ãã
ããããã¹ãã§ãããï¼
ã¾ã£ããéã®ã¢ã¼ããã¯ãã£ã§ããSQLãAPãµã¼ãã§å®è¡ãããModelã«å ¥ããã¨ãããã¨ã«ç¡çãããã ãªãã¸ã§ã¯ãæåãæ£ããç解ãã¦ããã°ãããã«ããéã£ãæ©è½ã¯ççµåã«ãããã¨ããMVCã®ç念ãç解ãã¦ããã°ãDBãµã¼ãã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ãç½®ãå¿ è¦ãããã¨åããã¯ãã§ãã
DBãµã¼ãã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ãç½®ãã¨ãã¦ãRDBMSã®ã¡ã¢ãªã¼ç©ºéã§ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãã¦æ±ããã®ã¯ã¹ãã¢ãããã·ã¼ã¸ã£ã«ãªãã¾ãã ã¤ã¾ãããAPIã¨ãã¦ã¹ãã¢ãããã·ã¼ã¸ã£ã使ãã¹ããã¨ããçµè«ã«ãªã訳ã§ãã
ãããªããªãã®ã¯èªåã®ã¹ãã«ã§ããã§ããã»ã§ããªããããã¹ã¿ã¼ããã¦ããããã§ãããï¼
ã¹ãã¢ãããã·ã¼ã¸ã£ã«ãããã¨ã«åé¡ã¯ãªãã®ï¼
ãã¡ãããåé¡ã¯ããã¾ãã ãã¹ã¦ã¹ãã¢ãããã·ã¼ã¸ã£ã«ããã«ã¯ãç¾ç¶ã®ä¸è¬çãªã¨ã³ã¸ãã¢ãæã£ã¦ããSQLãæ¸ãã¹ãã«ã§ã¯å ¨ã足ãã¾ããã
ã»ã¨ãã©ã®ã¨ã³ã¸ãã¢ã¯ãSQLã«ããã¦Staticããããããã²ã©ãããã§ãã
ãã®åé¡ã¯SQLã®ã¹ãã«ã端çã«æ¸¬ãã¾ãã åºæ¬æ§æã®åé¡ã§ãããããããééã人ã§RDBMSã«ä¿ã£ã¦ããã¨ãã¦ãéãè²°ãã®ã¯è©æ¬ºã¨è¨ã£ã¦ãè¯ãããã©ãããããªã¨ããã§ãã£ã¦ãããã¾ãããæ£è§£çã¯5ï¼ ãããã§ããStaticãããããæ¹å¤ãã¦ããæ¬äººããStaticããããããã¯ããã«åé¡ã®ãLEFT JOIN決ãæã¡ãããã£ã¦ãã¾ãã
ä¸ç10åãµã¤ãï¼ãã¼ã¸ãããªããï¼ã§ä½¿ããã¦ããWordPressã®å¤æãè¦ã¦ãåããéããæ¥æ¬ã ãã§ã¯ãªãä¸ççã«åãã£ã¦ãã¾ããã
Fetch ãã¦ã°ã«ã°ã«ããªã
ã¹ãã«ã足ããªã人ã«ã¨ã£ã¦ã¯ãã¹ãã¢ãããã·ã¼ã¸ã£ã«ããã¨è¨ãã¨ã
ãã¹ãã¢ãããã·ã¼ã¸ã£ã¿ãããªå®æ度ã®ä½ãè¨èªï¼ãã£ã¡ãä½ãï¼ã§ Fetch ãã¦ã°ã«ã°ã«ããããã£ã¡ã大å¤â¦â¦ã
ã¨èããã®ã§ãããã
確ãã«ã¹ãã«ã足ããªã人ãããFetch ãã¦ã°ã«ã°ã«ããããããªãã¨ããããã·ã¹ãã ã¯ä¸ç¬ã§ç ´ç¶»ãã¾ãã
å¼ç¤¾ã§åºå¹¹ã·ã¹ãã ããã¹ã¦ã¹ãã¢ãããã·ã¼ã¸ã£ã§è£½é ããã¨ããã¡ãããã¾ã£ããFetchããªãããã§ã¯ããã¾ããããFetchããã®ã¯1ï¼ ãããã§ãã SQLã®ã¹ãã«ãããã°ãFetchããªãã¨ãããªãå¦çã¯ã»ã¨ãã©ããã¾ããã
ä¾ãã°ãåºå¹¹ã·ã¹ãã ã§Fetchããæ©è½ã«ã¯ãè«æ±æ¸ãä½ãæ©è½ãããã¾ãã ã¨è¨ã£ã¦ããå¾æå ã³ã¼ããæå®ãã¦è«æ±æ¸ãä½ãå¦çã¯Fetchããå¿ è¦ã¯ããã¾ããã ããããç· ãæ¥ãæå®ãã¦ä¸æ¬ã§è«æ±æ¸ãä½ãå ´åãæå®ãããç· ãæ¥ã®å¾æå ã®ä¸è¦§ãåå¾ããå ã»ã©ã®å¾æå ã³ã¼ããæå®ãã¦è«æ±æ¸ãä½ãã¹ãã¢ãããã·ã¼ã¸ã£ã«Fetchãã¦æ¸¡ãã¾ãã
ãããããã®ãæ°ãã¦ã1ï¼ ãããã§ãã
ãå¾æå ã³ã¼ããæå®ãã¦ã1件åã®è«æ±æ¸ãä½ãã ããªãFetchããªãã§SQLã§å¦çã§ããã
ã¨ããã¹ãã«ãå¿ è¦ã§ãã ãã®ã¹ãã«ãççã«é«ããã¨ããã¨ãç¾ç¶ã§ã¯é«ãã§ãããã ããããå¼ç¤¾ã®ã»ããã¼ã§ã¯ãããã°ã©ãã³ã°æªçµé¨ã®äºåå¡ããã3æ¥ãããã°ã§ããããã«ãªãã¾ãã
æååé£çµã¯è¦ããªã
ãã¹ãã¢ãããã·ã¼ã¸ã£ã¿ãããªå®æ度ã®ä½ãè¨èªï¼ãã£ã¡ãä½ãï¼ã§æååé£çµãã¦SQLãæ§ç¯ãããªãã¦â¦â¦ã
ã¨æã人ãããã®ã§ãããã å ã»ã©ã®è«æ±æ¸ãä½æããSQLãªã©ãã¨ã¦ãè¤éãªå¦çããã¦ãã¾ãããæååé£çµã§SQLãæ§ç¯ãããã¨ã¯å¼ç¤¾ã§ã¯ã»ã¼ããã¾ããã ãã¡ãã«è©³ããæ¸ãã¾ããã
ã©ã¡ãã極端ã§ããï¼
ããã¹ã¦ã¹ãã¢ãããã·ã¼ã¸ã£ã«ããã¹ããã¨ããã¨ã極端ã ã¨è¨ããã¾ãããã©ã¡ãã極端ã§ããï¼
ã¾ã£ããçéã®ã¢ã¼ããã¯ãã£ã¼ã§ããSQLãããªãã¸ã§ã¯ãæåè¨èªã®ä¸ã§åççæããã å·éã«èããã°ããããªæ¥µç«¯ãªãã¨ã¯ããã¾ããã
ã»ã¨ãã©ã®RDBMSã¯ããªãã¸ã§ã¯ãæåè¨èªã®C++ã§æ¸ããã¦ãã¾ãã CPUã¯SQLãªãã¦ç解ã§ãã¾ããããããªãã¸ã§ã¯ãæåè¨èªã§æ¸ãããRDBMSã翻訳ããã¦ããã®ã§ãã
翻訳ãããçµæã¯ããå®è¡è¨ç»ãã¨ãã¦è¦ããã¨ãã§ãã¾ãã
åãSQLã§ããå®è¡è¨ç»ãå¤ããSQLã®ä¾ã
å®è¡è¨ç»ããªãã¸ã§ã¯ãæåè¨èªã«ç´ãã¨ãããªæãã«ãªãã¾ãã
SQL â å®è¡è¨ç» â ãªãã¸ã§ã¯ãæåè¨èª
ã¨ããå¤æãã§ããããã«ãªãã°ï¼ã¨ãããããããã§ããªã人ãã¨ã³ã¸ãã¢ã§è¯ãã®ãï¼ï¼
ãªãã¸ã§ã¯ãæåè¨èª â SQLï¼èªåçæï¼â ãªãã¸ã§ã¯ãæåè¨èªï¼èªåçæï¼
ã¨ãããå®è¡æã«ãè»è¼ªã®åéçºããèµ·ãã¦ãã¦ãã便å©ã ãã¨æ¥µç«¯ãªãã¨ãèããã¯ãããªãã§ãã ãããªãã¨ãèãã¦ãã¾ãã®ã¯ããSQLã®ã¹ãã«ãå§åçã«è¶³ããªãããã¨ã証æãã¦ããã ãã®è©±ã§ãã
åé¢éçºããã¹ã
SQLã¨ãªãã¸ã§ã¯ãæåè¨èªã¯ãçéã®ã¢ã¼ããã¯ãã£ã§ãã ç§ã¯ã©ã¡ããåãã«ã§ãã¾ãããããã§ãåæã«èããã®ã¯å°é£ã§ãã
ããã ãSQLãã§ããªãã¨ã³ã¸ãã¢ãããã®ã§ããããSQLãæ å½ããã¨ã³ã¸ãã¢ã¨ããªãã¸ã§ã¯ãæåè¨èªãæ å½ããã¨ã³ã¸ãã¢ãåããæ¹ãããã ã¹ãã¢ãããã·ã¼ã¸ã£ã«ããã°ãããå¯è½ã«ãªãã®ã§ãã
SQLã¨ããªãã¸ã§ã¯ãæåè¨èªãæ å½ããã¨ã³ã¸ãã¢ãåãããã°ããªãã¸ã§ã¯ãæåè¨èªãæ å½ããã¨ã³ã¸ãã¢ã¯SQLãç¥ãå¿ è¦ããªãã
ç¾ç¶ã§ã¯ãå ¨å¡ãSQLãç解ãã¦ããå¿ è¦ãããã ãããªæ¥µç«¯ãªãã¨ã¯ãããæ¹ãè¯ãã§ãããã
ãããã¼ã°çãSQLã®è¦æãå æããããªã³ã©ã¤ã³ã»ããã¼
以åããè¡ã£ã¦ããã¾ãããSQLã®è¦æãå æããã»ããã¼ãã®ãªã³ã©ã¤ã³çãä½ãã¾ããã
ãã®ã»ããã¼ã¯4æé以ä¸ãããããåæ¥ã®éå¬ã«ãªãã®ã§ãããååã®å 容ããããã¼ã°çã¨ãã¦9æ30æ¥ï¼æ°´ææ¥ï¼19æãã1æé30åï½2æéç¨åº¦ã§éå¬ãããã¾ãã
ãªã³ã©ã¤ã³ã»ããã¼ã«ãªãã¾ããããé æ¹ã®æ¹ããåå ãã ããã
ãããããé¡ããããã¾ãã
SQLã§æ¶è²»ç¨ã®å¦ç
ãã¡ãã§æ¸ããè¨äºã®ãæè¦ãæ°ã«ãªã£ãã®ã§å°ãã
ç®æ¬¡
- ç®æ¬¡
- ãæè¦
- ãã¼ãã«è¨è¨ã«ãããæ¶è²»ç¨çã«ã¤ãã¦
- ãã¹ã¿é¡ã®è¨è¨ä¾
- ãã©ã³ã¶ã¯ã·ã§ã³é¡ã®è¨è¨ä¾
- æ¶è²»ç¨çã®ç»é²ã®ä»æ¹
- SQLã§å¦çããã¨ãããªã
- ã¼ãã«ããã¨ãã«ã¯ãã¼ããæããã°è¯ã
- ãã©ã³ã¶ã¯ã·ã§ã³ã«ç¨çãå ¥ãããï¼
ãæè¦
> 99%ã¯åæãããä¸ã¤è¨ããã¦ã»ãããSQLã®æ大ã®å¼±ç¹ã¯æéã®æ±ãã«å¼±ããã¨ã§ãRDBã®çè«çåºç¤ã®æ°å¦ãæéãèæ ®ãã¦ãªããããããããªãã¨ã¯ããæ¶è²»ç¨ã¿ãããªæéã§å¤åãããã¹ã¿ã¸ã®é æ ®ãç¡ããããã
ã¨ã®ãã¨ã§ãããWindowé¢æ°ä»¥åã¯ãåå¾ã®ã¬ã³ã¼ãã®å¦çãèæ ®ããã¦ãã¾ããã§ãããç¾å¨ã¯ãMySQLã§ãWindowé¢æ°ãå®è£ ãããã®ã§ãã»ã¨ãã©ã®åé¡ã¯è§£æ±ºãããã¨æãã¾ãã
ããããæ¶è²»ç¨çã®å¤æ´ç¨åº¦ã§ããã°ãé常ã¯ãã¼ãã«ã¨ãã¼ã¿ãæ´åãããã¨ã§å¦çãå¯è½ã§ãã
ãã¼ãã«è¨è¨ã«ãããæ¶è²»ç¨çã«ã¤ãã¦
æ¶è²»ç¨ã§èæ ®ãã¹ãç¹ã¯ãç¨çã¯ã3種é¡ï¼é常ã軽æ¸ç¨çãé課ç¨åç®ï¼ããã顧客ï¼è²©å£²ç¸æï¼ã«2種é¡ï¼èª²ç¨å¯¾è±¡ã課ç¨å¯¾è±¡å¤ï¼ãããã¨ã«ããã¾ãã
ç¨çã«ã¤ãã¦ã¯ãååã®ã«ãã´ãªã¼ã«å¯¾ãã¦ç¨ç種å¥ãä»ããã¹ãã§ãããã
ãã¹ã¿é¡ã®è¨è¨ä¾
ãã©ã³ã¶ã¯ã·ã§ã³é¡ã®è¨è¨ä¾
æ¶è²»ç¨çã®ç»é²ã®ä»æ¹
é課ç¨åç®ããæ¶è²»ç¨ããªãã£ãæ代ã®ãã¼ã¿ã¾ã§ç¨çãã¼ãã§ç»é²ãã¦ãããã¨ããã½ã§ãï¼
SQLã§å¦çããã¨ãããªã
ä¸ã®ãããªãã¼ãã«è¨è¨ã«ãã¦ãæ¶è²»ç¨çãç»é²ãã¦ããã¨ãSQLã§å¦çããã¨ãã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
SELECT -- ä¸ç¥ , ss.å価 * ss.åæ° * tm.ç¨ç AS æ¶è²»ç¨é¡ -- ä¸ç¥ FROM 売ä¸ãã¼ãã« sm INNER JOIN 売ä¸æç´°ãã¼ãã« ss ON sm.ID = ss.売ä¸ID INNER JOIN 顧客ãã¹ã¿ cm ON sm.顧客ID = cm.ID INNER JOIN ååãã¹ã¿ pm ON ss.ååID = pm.ID INNER JOIN åååé¡ãã¹ã¿ pc ON pm.åååé¡ID = pc.ID INNER JOIN æ¶è²»ç¨çãã¹ã¿ tm ON sm.売ä¸æ¥ >= tm.é©å¿éå§æ¥ AND sm.売ä¸æ¥ <= tm.é©å¿çµäºæ¥ AND pc.æ¶è²»ç¨åºåID = tm.æ¶è²»ç¨åºåID WHERE -- 以ä¸ç¥ ;
CASEå¼ããå ¥ããªãã¨ãããã¨ã«æ³¨ç®ãã¦ãã ããã
ã¼ãã«ããã¨ãã«ã¯ãã¼ããæããã°è¯ã
ãã®ããã°ã§ããä½åº¦ãæ¸ãã¦ãã¾ããã
ã»ã¼ããå¾ããã¨ãã«ã¼ããæãã
ã»å¤åãªããå¾ããã¨ãã«ã¼ãã足ãããããã¯1ãæãã
ãªã©ã®å¦çã¯ãèªç¶è¨èªã§ã¯è¡ãããªã表ç¾ã§ãããæ°å¼ã«ããã¨ãã«å·¥å¤«ãã¹ãã§ãã
ã¤ã¾ããä»æ§ã決ããã¨ãã
ã顧客ã課ç¨å¯¾è±¡ã®ã¨ãã¯ç¨çåºåã®ç¨çãé©ç¨ãã課ç¨å¯¾è±¡å¤ï¼æµ·å¤ä¼æ¥ãªã©ï¼ã®ã¨ãã¯æ¶è²»ç¨é¡ãã¼ãã¨ããã
ãªã©ã¨ããä¼è©±ããªããã¾ããããããã®ã¾ã¾ããã°ã©ã ã«ããã¨ãã¨ã¦ãè¤éãªå¦çãå¿
è¦ã«ãªãã¾ãã
æ¶è²»ç¨èª²ç¨åºåï¼ååã¯èª²ç¨ä¿æ°ã®æ¹ãè¯ãããï¼ã0ã¨1ã«ãã¦ããã°ã
å価 * åæ° * ç¨ç * æ¶è²»ç¨èª²ç¨åºå
ã¨ããåç´ãªæ°å¼ã§ããã¹ã¦ã®çµåãã®å¦çãå¯è½ã«ãªãã¾ãã
ãã©ã³ã¶ã¯ã·ã§ã³ã«ç¨çãå ¥ãããï¼
ç¨çã¯é常ã¯å°åºé
ç®ã«ãªãã¾ãã
ããããç¨çãå¤æ´ã«ãªãåå¾ã§ãç¹å¥ã«åã®ç¨çã§å¦çãã¦æ¬²ãããªã©ã¨ããã¤ã¬ã®ã¥ã©ã¼ãªãã¨ãè¨ãåºãå¯è½æ§ãããã¨ãã«ã¯ããã©ã³ã¶ã¯ã·ã§ã³ã«éæ£è¦åãã¦ãããæ¹ãè¯ãã§ãããã
ï¼å£²ä¸æ¥ãåæ¥ã«ãã¹ãã§ããã©ãï¼
MySQLã§ãµãã¯ã¨ãªãã©ããã¦ãé ãã¨ãã®å¯¾å¦æ³
ä»ã®RDBMSã§ãèµ·ããã¾ãããç¹ã«MySQLã§ã¯ããµãã¯ã¨ãªã使ãã¨ã©ããã¦ãé
ãã¨ããããã¾ãã
ãããªã¨ãã®å¯¾å¦æ³ã¯ä¸»ã«2ç¹ããã¾ãã
ç®æ¬¡
ãã³ãã©ãªã¼ãã¼ãã«ã使ãæ¹æ³
DROP TEMPORARY TABLE IF EXISTS tmp1; CREATE TEMPORARY TABLE tmp1 -- (PRIMARY KEY(id)) å¿ è¦ã«å¿ãã¦ä¸»ãã¼ -- (INDEX(id)) ã¤ã³ããã¯ã¹ãçæãã AS SELECT * FROM customer; -- 使ãããã£ããµãã¯ã¨ãª -- é ãã£ãã¯ã¨ãªã®ãµãã¯ã¨ãªã tmp1 ã«ãã¦æ¸ãæããã DROP TEMPORARY TABLE IF EXISTS tmp1;
æååé£çµã§SQLãçæããæ¹æ³
ä¾ãã°ã以ä¸ã®ãããªã¨ããcol1ã®ã¤ã³ããã¯ã¹ã使ã£ãæ¹ãæ©ãã¨ãã§ãããã³ããå ¥ãã¦ã使ã£ã¦ãããªããã¨ãããã
SELECT * FROM table1 WHERE col1 IN (SELECT colx FROM table2 WHERE xxx) ;
対å¦æ³
SELECT colx FROM table2 WHERE xxx ;
ãå ã«å®è¡ããçµæãã
SELECT * FROM table1 WHERE col1 IN (1, 3, 5) ;
SELECT * FROM table1 WHERE col1 IN ('JPN', 'USA') ;
ãªã©ã®SQLæãçæããã
é åã使ããDBã§ããé åã使ã£ããã¤ã³ããã¯ã¹ã使ããªãã£ããï¼å¤ããã¼ã¸ã§ã³ã®è¨æ¶ãªã®ã§ç¾å¨ã¯ã©ããï¼ï¼ããã¨ãã«ãæå¹ã§ãã
ã²ã¼ã ãé¡æã«å¦ã¶ å é¨æ§é ããç解ããMySQL
以åãSoftware Design ã®ç¹éè¨äºãå·çãã¾ãããã
æè¡è©è«ç¤¾ã®ãã¼ã¸ã§ãã¹ã¦ç¡æã§å ¬éããã¦ãã¾ãã
ãã²ãã覧ãã ããã
ãSQLã®è¦æãå æãããã»ããã¼
ãæéãåãã¾ããããåå ãã ããã
æèãSQLã®è¦æãå æããæ¬ãã®ãæ¡å
https://www.amazon.co.jp/gp/product/4297107171
ããã°ã§æ¸ãã¦ããå 容ãããã£ã¨åããããããããã®ã§ãã
ä¾ãã°ã以ä¸ã®è¨äºã®å 容ããåããããã解説ãã¦ãã¾ãã
ãSQLãæååé£çµãã¦ä½ãå¿ è¦ã¯ãªãã
ç§ã¯ã©ããã¦ãããèªåãç解ãã¦ããç¯å²ãã¨ã³ã¸ãã¢ãªãç解ãã¦ããã ãããã¨ããæ³ãã§æç« ãæ¸ãã¦ãã¾ãã¾ãã ããããããã§ã¯é£è§£ã§ç解ã§ããªãã¨ã³ã¸ãã¢ãå¤ãã¨ãããã¨ã§ãéç±³ç浩ããã«ç¬¬ä¸è ã«ããããããããªã©ã¤ããã¦ããã ãã¦ãã¾ãã
æ¯éããè³¼å ¥ããã ããã°å¹¸ãã§ãã