ãç¸é¢ãµãã¯ã¨ãªãã¨ã¯ä½ããç解ãã¦ï¼è¤éãªSQLã§ãèªããããã«ãªãã
SQLã®ãç¸é¢ãµãã¯ã¨ãªãããããã°ã»ã»ã»
- 巨大ãªSQLãï¼è¿·ããã«èªããããã«ãªãã
- ãé¢æ°ãã®ãããªï¼ä¾¿å©ãªãµãã¯ã¨ãªãæ¸ããããã«ãªãã
以ä¸ã§ã¯ï¼
- ãç¸é¢ãµãã¯ã¨ãªãã¨ã¯ä½ãï¼
- æ®éã®ãµãã¯ã¨ãªï¼éç¸é¢ãµãã¯ã¨ãªï¼ãJOINæä½ã¨ã¯ä½ãéãã®ãï¼
- å¤éã«ãã¹ããããï¼å·¨å¤§ãªSQLã®èªã¿æ¹ã¯ï¼
ã¨ããç¹ãè«ããã
ãµã³ãã«ãã¼ã¿ï¼ããã³å ¨ä½ã®æ¹é
ã¾ãï¼ç¸é¢ãµãã¯ã¨ãªã®èª¬æã®ããã«ï¼ä»¥ä¸ã®ãããªãã¼ãã«ãä¾ã¨ãã¦åãä¸ããã
- table1ãï¼æ®éã®ãã¼ã¿
- table2ãï¼ãã¹ã¿ãã¼ã¿ï¼ãã¯ã¤ããªã¹ãï¼
ããã¹ã¿ã«ä¸è´ããªãã¬ã³ã¼ããã¯ãããã¨ããæä½ããããã
æ¹æ³ã¯ï¼ãã¿ã¼ã³ããã
- ãµãã¯ã¨ãªç¡ããâ¦ï¼ï¼ï¼
- ãµãã¯ã¨ãªæã
- INã§éç¸é¢ãµãã¯ã¨ãªããâ¦ï¼ï¼ï¼
- EXISTSã§ç¸é¢ãµãã¯ã¨ãªãâ¦ï¼ï¼ï¼
ï¼ï¼ï¼ãµãã¯ã¨ãªç¡ãã§JOIN
JOINã®æå³ãã¤ã¡ã¼ã¸ã復ç¿ããã
SELECT table1.fuga FROM table2 LEFT JOIN table1 ON table2.hoge = table1.hoge ;
table2åªä½ã§JOINããã
table2ã¨ãããã¹ã¿ï¼ãã¯ã¤ããªã¹ãï¼ã許å¯ãããã®ã ãï¼table1ã®ä¸èº«ã¯çãæ®ãã
table1ã«å¯¾ããï¼ä¸ç¨®ã®ããªãã¼ã·ã§ã³ï¼ã¾ãã¯ãã£ã«ã¿ï¼ã
表ã¨è¡¨ãå·¦å³ããè¡çªãã¦ï¼ä¸é©åãªã¬ã³ã¼ããå³å´ãããã£ãºãã«æãè½ã¡ããããªã¤ã¡ã¼ã¸ã
åªä½ãªå·¦å´ã®è¡¨ããåºãã表ã§ï¼å³å´ã®è¡¨ã¯ãã®åºãã«è² ããã
table1ãå³å´ããtable2ã«åãã£ã¦ãã¶ã¤ãã£ã¦ãããã®ã ãï¼ãã®éï¼table2ã«ä¸è´ããªãç©ã¯ã¯ããããã
JOINã¨ã¯ãæ å ±ä»å ãã§ããï¼ã¨ã¿ãªããã
ããã§ã¯ï¼table2ã¨ãããã¯ã¤ããªã¹ãã«å¯¾ãã¦ï¼å³å´ããtable1å ã®æ å ±ãä»å ãã¦ããã
ä¸å¿ã¯ããã¾ã§ãã¯ã¤ããªã¹ãã§ããç¹ã«æ³¨æã
ï¼ï¼ï¼INã§éç¸é¢ãµãã¯ã¨ãª
ãµãã¯ã¨ãªã使ã£ã¦ï¼åæ§ã®æä½ããã¦ã¿ãã
ããã¯ç¸é¢ãµãã¯ã¨ãªã§ã¯ãªãï¼æ®éã®ãµãã¯ã¨ãªï¼éç¸é¢ãµãã¯ã¨ãªï¼ã§ããã
SELECT table1.fuga FROM table1 WHERE table1.hoge IN ( SELECT table2.hoge FROM table2 ) ;
table2ã¨ãããã¯ã¤ããªã¹ããï¼ã¾ãï¼ã¤ã®è¡¨ã¨ãã¦ç¢ºç«ãããï¼ãµãã¯ã¨ãªå ãï¼
ãã®å®ææ¸ã¿ã®ãã¯ã¤ããªã¹ãã«ç §ããåãããªããtable1ãï¼ä»¶ãã¤ã¹ãã£ã³ãã¦ï¼çãæ®ããã©ãããé ã«å¤å®ãã¦ããã
table1ã®ãã¡ï¼ãã¯ã¤ããªã¹ãã«è¼ã£ã¦ããªããã®ã¯æ¨ã¦ãããã
ï¼ï¼ï¼ã§ã¯å·¦å³ãããã¼ãã«ãè¡çªãã¦ï¼ãã£ãºãã«ç¡å¹ãã¼ã¿ãæãè½ã¡ããããªåçãªã¤ã¡ã¼ã¸ã ã£ãã
ï¼ï¼ï¼ã¯ï¼ï¼ï¼ã¨éã£ã¦ï¼table1ã®åã¬ã³ã¼ãã®å¯å¦ããã¾ããã«ï¼ä»¶ï¼ä»¶ï¼ãã¯ã¤ããªã¹ãtable2ãè¦ãªãããã§ãã¯ãã¦ããã¤ã¡ã¼ã¸ã
å ¨è¬çã«ãµãã¯ã¨ãªã¯ï¼æåï¼table2ã«é¢ããã話é¡ã確ç«ããã¦ããï¼ã¾ããã¨table1ã«ãå¤æããã¦ãããããªã¤ã¡ã¼ã¸ã
ããã°ãã¼ãã«å¤æã
話é¡ã®ä¸å¿ãtable2ãã¼ãã«ããtable1ãã¼ãã«ã«ãããã
ã ããï¼ãµãã¯ã¨ãªãããå ´åã¯
- ã¾ãï¼ãµãã¯ã¨ãªã®ä¸èº«ãèªãã§ãã
- 次ã«ï¼ãã®å¤å´ãèªã
ã¨ãããµãã«èªã¿é²ãã¦ããã°ï¼é·ãSQLã§ãã£ã¦ãç解ã§ããã
ååçã«ï¼å å´ããå¤å´ã«åãã£ã¦æ å ±ãæµãã¦ããï¼ã¨ãããã¨ã ã
â»ãã ãï¼ï¼ï¼ã§å¾è¿°ããã¨ããï¼ç¸é¢ãµãã¯ã¨ãªã®å ´åã«ã¯ï¼ããã¯ãããªããªãã
â»ãªãï¼ããï¼NOT INã§å¯è¡¨å ã«NULLãå ¥ã£ã¦ããã¨ï¼æãéãã®çµæãå¾ãããªãããNULLã§ã¯ãªãç©ããçãæ®ãï¼ã®ã§ã¯ãªãã
http://www.geocities.jp/mickindex/dat...
NOT IN ã®ãµãã¯ã¨ãªã§ä½¿ç¨ããããã¼ãã«ã®ãã¼ã« NULL ãå«ã¾ãã¦ããå ´åãçµæã¯å¿ ã空ã«ãªãã®ã§ãã
ï¼ï¼ï¼ã®è£è¶³ï¼ãµãã¯ã¨ãªããé¢æ°ãã¨èãã¦ã¿ãã
ä¸ã®SQLã® table1.hoge IN ï¼ãµãã¯ã¨ãªï¼ ã®é¨åã«ã¯ï¼ããã¹ã¿ã¬ã³ã¼ããè¿ãé¢æ°ãã®ãããªãã®ãåå¨ããï¼ã¨èããã¨ããããããã
( ãµãã¯ã¨ãª )ãã表ããã®ï¼
- å¼æ°ï¼ãªãã
- è¿ãå¤ï¼table2ã®å ¨ã¬ã³ã¼ãã
ã¨ããé¢æ°ã
å¼æ°ãç¡ãã®ã§ï¼ã¡ã¤ã³ã¯ã¨ãªã«é¢ä¿ãªãï¼ãã¤ãåãçµæãè¿ãã
ã ããï¼ï¼åã ãå®è¡ããã°ããã
table1.hoge IN ( ãµãã¯ã¨ãª )ãã表ããã®ï¼
- å¼æ°ï¼table1ã®ï¼ä»¶ã®ã¬ã³ã¼ããï¼ã¾ãã¯ï¼table1ã®ï¼ä»¶ã®ã¬ã³ã¼ãã®ãhogeãã®å¤ãï¼
- è¿ãå¤ï¼çå½å¤ã
ã¨ããé¢æ°ã
ãµãã¯ã¨ãªã®è¡¨ããé¢æ°ãã«ã¯å¼æ°ãç¡ãã
å¼æ°ãå¤ãã¦ç¹°ãè¿ãå®è¡ããå¿
è¦ãç¡ãã®ã§ï¼ï¼åã ãå®è¡ããã°ï¼ãã¨ã§çµæã使ãã¾ãããã
ï¼ï¼ï¼EXISTSã§ç¸é¢ãµãã¯ã¨ãª
ããã§ï¼ç¸é¢ãµãã¯ã¨ãªãç¾ããã
ä»ã¾ã§ã¨åãäºãï¼ç¸é¢ãµãã¯ã¨ãªã使ã£ã¦è¡ãªã£ã¦ã¿ãã
SELECT table1.fuga FROM table1 WHERE EXISTS ( SELECT * FROM table2 WHERE table1.hoge = table2.hoge ) ;
EXISTS ( ãµãã¯ã¨ãª ) ã®é¨åã¯ãé¢æ°ãã®ãããªãã®ã§ããï¼ã¨èããã¨ããããããã
( ãµãã¯ã¨ãª )ãã表ããã®ï¼
- å¼æ°ï¼table1ã®ï¼ä»¶ã®ã¬ã³ã¼ããï¼ãããã¯ï¼ãããåã ã®ãtable1.hogeãã®å¤ãï¼
- è¿ãå¤ï¼table2ã®ï¼çµè¾¼ã¿æ¸ã¿ã®è¤æ°ä»¶ã®ã¬ã³ã¼ãã
ã¨ããé¢æ°ã
EXISTS ( ãµãã¯ã¨ãª )ãã表ããã®ï¼
- å¼æ°ï¼table1ã®ï¼ä»¶ã®ã¬ã³ã¼ããï¼ãããã¯ï¼ãããåã ã®ãtable1.hogeãã®å¤ãï¼
- è¿ãå¤ï¼çå½å¤ã
ã¨ããé¢æ°ã
ï¼ï¼ï¼ã®ãµãã¯ã¨ãªã®è¡¨ããé¢æ°ãã«ã¯å¼æ°ãããã®ã§ï¼table1ã®åè¡ãã¨ã«ï¼æ¯åå®è¡ããå¿ è¦ãããã
ãµãã¯ã¨ãªã®ä¸ã§WHEREã§æ¡ä»¶ãçµãè¾¼ãã§ãããã©ããï¼ã¨ããç¹ã§ï¼ï¼ï¼ï¼ã¨ï¼ï¼ï¼ã¯ç°ãªãã
ãã®å ´åï¼ãããªããµãã¯ã¨ãªã®å å´ããèªã¿é²ãããã¨ãã¦ãï¼ãã¾ããããªãã
FROMå¥å ã«ã¯table2ã¨ããæ¸ãã¦ããªãã®ã«ï¼ãtable1ãã¨ããã©ãã«ãæ¸ããã¦ããªããã¼ãã«ããããªãWHEREå¥å ã«åºç¾ããããï¼æ¸æã£ã¦ãã¾ãã®ã ã
ãããå½ç¶ãtable1ã¨ããã®ã¯ï¼ãµãã¯ã¨ãªãåãåããå¼æ°ãã®ãããªãã®ã ããï¼ãµãã¯ã¨ãªã®å¤å´ãè¦ãªãéãï¼table1ã®æå³ã¯ããããªãã®ã ã
ãããç¸é¢ãµãã¯ã¨ãªã
ãµãã¯ã¨ãªã®å å´ã¨å¤å´ãï¼äºãã«é¢é£ããã£ã¦ããã®ã§ããã
ãµãã¯ã¨ãªã®ä¸èº«ããé¢æ°ãã¨ã¿ãªããå ´åã»ã»ã»
- éç¸é¢ãµãã¯ã¨ãªã®å ´åã¯ï¼ä¸åº¦å®è¡ããã°ããã
- ç¸é¢ãµãã¯ã¨ãªã®å ´åã¯ï¼æ¯åå®è¡ããå¿ è¦ãããã
ä»ã®ãµã³ãã«
ç¸é¢ãµãã¯ã¨ãªã®èªã¿æ¹ã®ç·´ç¿ã
ãµãã¯ã¨ãªã®åºæ¬ã7.5. ç¸é¢ãµãã¯ã¨ãª
http://www.techscore.com/tech/sql/07_...
ã® sample.18-6
ãã®å ´åï¼ãµãã¯ã¨ãªã表ãã¦ããé¢æ°ã¯ã»ã»ã»
- å¼æ°ï¼AAã®ï¼ä»¶ã®ã¬ã³ã¼ãã®ååã³ã¼ãã
- è¿ãå¤ï¼è©²å½ååã®ç´åæ¥ã
ã¨ããé¢æ°ã
ååã³ã¼ãã渡ãã¨ç´åæ¥ãè¿ãã¦ããããããªãé¢æ°ããä½ã£ã¦ããã¦ï¼
ããã«ãã®è¿ãå¤ã'20010401'ã«ãªããã©ããããã§ãã¯ãã¦ããã
ç¸é¢ãµãã¯ã¨ãªã§è¡ã¨è¡ãæ¯è¼ãã
http://codezine.jp/article/detail/907
ã®ï¼æåã«åºã¦ããSQLï¼åå¹´ã¨å¹´åãåã年度ãæ±ããï¼
ãã®å ´åï¼ãµãã¯ã¨ãªã表ãã¦ããé¢æ°ã¯ã»ã»ã»
- å¼æ°ï¼S1ã®ï¼ä»¶ã®ã¬ã³ã¼ãã®å¹´åº¦ã
- è¿ãå¤ï¼è©²å½å¹´åº¦ã®åã®å¹´ã®å£²ãä¸ãã
ã¨ããé¢æ°ã
年度ã渡ãã¨åã®å¹´ã®å£²ãä¸ããè¿ãã¦ããããããªãé¢æ°ããä½ã£ã¦ããã¦ï¼
ããã«ãã®è¿ãå¤ã該å½å¹´åº¦ã®å£²ãä¸ãã¨ä¸è´ãããã©ããããã§ãã¯ãã¦ããã
巨大SQLã®èªã¿æ¹
ãµãã¯ã¨ãªãä½éã«ããã¹ãããã¦ãããããªï¼å·¨å¤§ãªSQLã®èªã¿æ¹ã
- ï¼ï¼ï¼æãå å´ã«ãã¹ãããããµãã¯ã¨ãªããï¼å¤å´ã«åãã£ã¦èªã¿é²ãã¦ããã
- å å´ã®çµæã使ã£ã¦ï¼å¤å´ã®è¨ç®ãè¡ãªããã¦ããããã
- ãã¤ãå å´ããå¤å´ã¸åãã£ã¦ãæ å ±ãæµãã¦ãããã
- ï¼ï¼ï¼åºæ¬çã«ã¯ï¼å¤å´ãèªãã§ããã¨ãã«ï¼å å´ãå度èªã¿ç´ãå¿ è¦ãçããªãã
- éç¸é¢ãµãã¯ã¨ãªã¯ï¼ä¸åº¦ããå®è¡ãããªãããã
- ãµãã¯ã¨ãªãä¸åçµæãåºãããï¼ãããã®ãµãã¯ã¨ãªãå度å®è¡ããå¿ è¦ã¯çããªãã
- ï¼ï¼ï¼å å´ããèªã¿é²ãã¦ãã£ãæã«ï¼æ¥ã«ãæªå®ç¾©ã®æ å ±ããç¾ããå ´åãããã
- ãããã®æ å ±ãå¤å´ã®ã¯ã¨ãªã§å®ç¾©ããã¦ããå ´åï¼ããã¯ç¸é¢ãµãã¯ã¨ãªã§ããã
- å å´ããå¤å´ã§ã¯ãªãï¼ããã¯ãã£ããéã«ï¼å¤å´ããå å´ã¸åãã£ã¦èªã¿é²ããå¿ è¦ãããã
é¢é£ããè¨äºï¼
ã¤ã³ããã¯ã¹ãä½æãã¦ï¼SQLã®é度ããã¥ã¼ãã³ã°ããæé ãï¼PostgreSQLã§ï¼EXPLAINæã¨CREATE INDEXæã«ããããã©ã¼ãã³ã¹æ¹åï¼
http://language-and-engineering.hatenablog.jp/entry/20110121/p1
ãã¼ã¿ãã¼ã¹ã¨SQLã®æ¥åã¹ãã«ã¬ãã«ãå¤å¥è¡¨ãï¼ï¼æ®µéï¼
http://language-and-engineering.hatenablog.jp/entry/20110320/p1
SQLã§ï¼ãã¼ãã«ã«åå¨ããªãè¤æ°è¡ã®ãã¼ã¿ãåå¾ããæ¹æ³ãï¼PostgreSQLã® generate_seriesé¢æ°ããã¹ã¿ã¼ãããï¼
http://language-and-engineering.hatenablog.jp/entry/20100309/p1
PostgreSQLã®ã·ã¹ãã ãã¼ãã«å
¥éãï¼æè¨ç¨ã®SQLéï¼
http://language-and-engineering.hatenablog.jp/entry/20100220/p1
ã