å®è¡è¨ç»ã®ãã£ãã·ã¥ã«ã¤ãã¦ï¼ - åå²ãé¿ããSQL
ãã¤ã¦æååé£çµããã¦ãSQLæãæ§ç¯ãã¦ããæ代ãããã¾ããã
ç¾å¨ã§ã¯ ORM ãå©ç¨ãããã¨ãå¤ãããããã¾ããã
ããããè«çæ¼ç®ããã£ããç解ãã¦å·¥å¤«ããã° SQL ã ã㧠WHEREå¥ãªã©ãåå²ããã«ä½ããã¨ãå¯è½ã§ãã
ç®æ¬¡
- ç®æ¬¡
- åå²ï¼IFæï¼ãè¨ç®å¼ã
- è«çæ¼ç®ã«ã¤ãã¦
- åå²ãé¿ããSQL
- ãã¼ã¹ãå¿ è¦ï¼å®è¡è¨ç»ã®ãã£ãã·ã¥ã使ããªãï¼
Â
Â
åå²ï¼IFæï¼ãè¨ç®å¼ã
ç§ã¯ããã°ã©ãï¼è¨è¨è ï¼ã®è³è³ªã«ãè¨ãæããå·§ãããã¨ãããã¨ãéè¦ãã¦ãã¾ãã
ä¾ãã°ãæ¶è²»ç¨çã8ï¼ ã§é課ç¨åããã£ãã¨ããååãã¹ã¿ã®é課ç¨åã®ç¨çé ç®ã« 0ã課ç¨åã«ã¯ 0.08 ãç»é²ãã¦ããã°ãåç´ãªè¨ç®ã§åé¡ããã¾ããã
ããããæ £ãã¦ããªãã¨æ¥æ¬èªã§ä»æ§æ¸ãæ¸ãã¨ãã«
é課ç¨åãã©ã°ã1ã®ã¨ãã¯ãæ¶è²»ç¨ã課ç¨ããªãï¼æ¶è²»ç¨ãã¼ãã«ããï¼Â
 ã®ããã«æ¸ããããããã®ã¾ã¾åå²ï¼IFæï¼ã§å¦çãã¦ãã¾ã人ãåºã¦ãã¦ãã¾ãã
æ¥æ¬èªã«éããã
- ï¼ãå¾ããã¨ãã«ã0 ãæãã
- å¤åãªããå¾ããã¨ãã1 ãæããï¼ã§å²ãï¼
- å¤åãªããå¾ããã¨ãã0 ã足ãï¼ãå¼ãï¼
ãªã©ã®å¦çã¯ãèªç¶è¨èªã¨ãã¦ã¯ããæé»ã®å¤æãèµ·ããã
ã¤ã¾ããæ¶è²»ç¨ã®ããã«ç¨çã0ï¼ ã¨8ï¼ ãããã¨ããã°ããã¨ããããé課ç¨åç®ã¨èª²ç¨åç®ã¨ãã風ã«è¡¨ç¾ããã¾ãã
ãã®ãããããã®éã®æé»ã®å¤æã
ã¤ã¾ããé課ç¨åç®ã¨ã¯ãç¨çã0ï¼ ã§ãããã¨è¨ãæããã§ããããã¨ããã®ã¯ITæè¡è ã«ã¨ã£ã¦é常ã«éè¦ãªè³è³ªã ã¨èãã¦ãã訳ã§ãã
è«çæ¼ç®ã«ã¤ãã¦
sikushima.hatenablog.comã§ãæ¸ãã¾ããããåºæ¬çã«
- ANDï¼è«çç©ï¼ã¯æãç®
- ORï¼è«çåï¼ã¯è¶³ãç®
- True 㯠ï¼ï¼ï¼ä»¥å¤ï¼
- False 㯠ï¼
ã¨ç½®ãæãã¦èãã¦åé¡ããã¾ããã
å±éãå æ°å解ãæ°å¼ã¨åãã«ãªãã¾ãã
ä¾ãã°æ°å¼ã§ãå±éããå¼ã¨ãå æ°å解ããå¼ã¯ã
 (a à b) ï¼ (a à c)
 = a à (b ï¼ c)Â
 æãç®ã AND ã足ãç®ã OR ã«ç½®ãæããè«çå¼ãåæ§ã«ãªãã¾ãã
(a AND b) OR (a AND c)Â
 = a AND (b OR c)          Â
 æ¥æ¬èªã®ä¾ã¨ãã¦ã¯ã
ç·æ§ã§æ¥æ¬äººãã¾ãã¯ãç·æ§ã§ã¢ã¡ãªã«äºº
ç·æ§ã§ãæ¥æ¬äººãã¾ãã¯ãã¢ã¡ãªã«äºº
æ¥æ¬èªã«éãããèªç¶è¨èªï¼ç¹ã«æ¥æ¬èªã¯ï¼ãå æ°å解ããã¨ãæåã®ãç·æ§ã§ããããã©ãã¾ã§ã«æãã£ã¦ããã®ãåããã«ããééãã®ãã¨ï¼ä¾ãã°ããçä¹³ãããã°ãã¨ããã¸ã§ã¼ã¯ï¼ã«ãªãã¾ãããçå±ã¯åãã§ãã
ããããï¼æèã®ä¸ãèªç¶è¨èªãã½ã¼ã¹ã³ã¼ãï¼ã¹ã©ã¹ã©ã§ãã¦ããªã人ã®ã½ã¼ã¹ã³ã¼ãã¯ããã¹ããæ·±ããªãããã©ãã¦é常ã«èªã¿ã«ãããªã£ã¦ãã¾ãã¾ãã
åå²ãé¿ããSQL
ãã¡ãããSQLã§ãåãã§ãä»ã®è¨èªã¨æ¯ã¹ãã¨SQLã«ã¯åå²ããªãããããã®å¿ç¨ãç¹ã«éè¦ã«ãªãã¾ãã
ã¡ããã¨ç解ãã¦ããã°ãã¹ã©ã¤ãã®éããåå²ããã¦WHEREå¥ãåççæããå¿ è¦ã¯ããã¾ãããÂ
ï¼â¦â¦OR NULL IS NULL)
ã¤ã¾ãã
ï¼â¦â¦OR True)
ã¨ãªãã¨ãã«ãã³ã®å å´ã«ã©ããªæ¡ä»¶ããã£ã¦ããå¿ ããTrueã«ãªãã¾ããããã«ã©ã ã®å 容ã確èªããå¿ è¦ããªã訳ã§ãã
ãã¼ã¹ãå¿ è¦ï¼å®è¡è¨ç»ã®ãã£ãã·ã¥ã使ããªãï¼
ä¸ã¤ã®SQLã§è¤æ°ã®å®è¡è¨ç»ã«ãªãã¨ãããã¨ã¯ãå®è¡è¨ç»ã®ãã£ãã·ã¥ã使ããªãã¨ãããã¨ã«ãªãã¾ãã
åç¨ãµã¼ãã§ã¯å®è¡è¨ç»ããã£ãã·ã¥ãã¦ãã¾ãã®ã§ãããã使ããªãã®ã¯ããã ããªãã§ããã
次åã¯ãå®è¡è¨ç»ã®ãã£ãã·ã¥ã®éãã¨ãææ¡ã«ã¤ãã¦æ¸ãã¾ãã
Â