{"id":14843,"date":"2021-11-07T11:28:01","date_gmt":"2021-11-07T02:28:01","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=14843"},"modified":"2023-05-27T23:14:39","modified_gmt":"2023-05-27T14:14:39","slug":"%e3%80%90sql%e3%80%91%e3%82%a2%e3%83%b3%e3%83%81%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3%e3%80%8cunion%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e6%9d%a1%e4%bb%b6%e5%88%86%e5%b2%90%e3%80%8d","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=14843","title":{"rendered":"\u3010SQL\u3011\u30a2\u30f3\u30c1\u30d1\u30bf\u30fc\u30f3\u300cUNION\u3092\u4f7f\u3063\u305f\u6761\u4ef6\u5206\u5c90\u300d"},"content":{"rendered":"<h2>\u30a2\u30f3\u30c1\u30d1\u30bf\u30fc\u30f3\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306bwhere\u53e5\u306e\u6761\u4ef6\u3060\u3051\u5c11\u3057\u5909\u3048\u3066UNION\u3067\u304f\u3063\u3064\u3051\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308bSQL\u3092\u3084\u3063\u3066\u3057\u307e\u3044\u304c\u3061\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">SELECT \u5217x\u3001\u5217y as \u5225\u540d\r\n  FROM \u30c6\u30fc\u30d6\u30eb1\r\n WHERE \u6761\u4ef6A\r\nUNION ALL\r\nSELECT \u5217x\u3001\u5217Z as \u5225\u540d\r\n  FROM \u30c6\u30fc\u30d6\u30eb1\r\n WHERE \u6761\u4ef6B;<\/pre>\n<h3>\u4f7f\u3044\u305f\u304f\u306a\u308b\u30b1\u30fc\u30b9<\/h3>\n<p>\u300c\u6761\u4ef6A\u3067\u306f\u3001\u5217y\u3092\u629c\u304d\u51fa\u3059\u3002\u6761\u4ef6B\u3067\u306f\u5217z\u3092\u629c\u304d\u51fa\u3059\u3002\u300d\u306a\u3069\u306e\u3088\u3046\u306a\u5834\u5408\uff08\u4f8b\u3048\u3070\u3001\u5e74\u5ea6\u306b\u3088\u3063\u3066\u4fa1\u683c\u6539\u5b9a\u304c\u3042\u308b\u306a\u3069\uff09<\/p>\n<h3>\u30c7\u30e1\u30ea\u30c3\u30c8<\/h3>\n<h4>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u9045\u5ef6<\/h4>\n<p>SQL\u306e\u767a\u884c\u56de\u6570\u3053\u305d1\u56de\u3068\u306f\u8a00\u3048\u3001\u5185\u90e8\u7684\u306b\u306f\u8907\u6570\u306eSELECT\u6587\u3092\u5b9f\u884c\u3059\u308b\u3082\u306e\u3068\u3057\u3066\u89e3\u91c8\u3055\u308c\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u30a2\u30af\u30bb\u30b9\u56de\u6570\u304c\u5897\u3048\u3066\u300c\u30b9\u30c8\u30ec\u30fc\u30b8\u306eI\/O\u7269\u7406\u30b3\u30b9\u30c8\u300d\u304c\u81a8\u3089\u3080\u50be\u5411\u306b\u3042\u308b\u3002<\/p>\n<p>\u307e\u305f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30b9\u30ad\u30e3\u30f3\u306a\u3089\u307e\u3060\u3057\u3082\u3001SQL\u306e\u6761\u4ef6\u306b\u3088\u3063\u3066\u306f\u30d5\u30eb\u30c6\u30fc\u30d6\u30eb\u30b9\u30ad\u30e3\u30f3\u304c2\u5ea6\u5b9f\u884c\u3055\u308c\u3066\u3057\u307e\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3057\u3001\u8aad\u307f\u53d6\u308a\u30b3\u30b9\u30c8\u306f\u30c7\u30fc\u30bf\u4ef6\u6570\u306b\u5fdc\u3058\u3066\u5897\u5927\u3057\u3066\u3044\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h4>\u8aad\u307f\u3065\u3089\u3044<\/h4>\n<p>\u307e\u305f\u3001\u305d\u3082\u305d\u3082\u4f3c\u305f\u3088\u3046\u306aSQL\u304c2\u5ea6\u767a\u884c\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306a\u69cb\u6587\u306b\u306a\u3063\u3066\u304a\u308a\u5197\u9577\u306b\u898b\u3048\u3066\u53ef\u8aad\u6027\u304c\u4f4e\u3044\u3067\u3059\u3002<\/p>\n<h2>CASE\u5f0f\u3092\u4f7f\u3063\u305f\u66f8\u304d\u63db\u3048<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u308b\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306f2\u500d\u306b\u4e0a\u304c\u308a\u307e\u3059\u3002\u5b89\u6613\u306bUNION\u306b\u983c\u3089\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">SELECT \u5217x,\r\n       CASE WHEN \u6761\u4ef6A THEN \u5217y\r\n            WHEN \u6761\u4ef6B THEN \u5217z END AS \u5225\u540d\r\n  FROM \u30c6\u30fc\u30d6\u30eb1;<\/pre>\n<p>WHERE\u53e5\u3067\u6761\u4ef6\u5206\u5c90\u3092\u3055\u305b\u308b\u3068\u3044\u3046\u8003\u3048\u65b9\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3067\u304d\u308b\u9650\u308aSELECT\u53e5\u3067\u6761\u4ef6\u5206\u5c90\u3055\u305b\u305f\u65b9\u304c\u7121\u99c4\u306a\u30b9\u30ad\u30e3\u30f3\u304c\u767a\u751f\u3057\u306a\u3044\u306e\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u826f\u304f\u306a\u308b\u306e\u3067\u305d\u306e\u8fba\u3082\u610f\u8b58\u3057\u3066\u5b9f\u88c5\u3092\u884c\u3048\u308b\u3068\u3055\u3089\u306b\u30d7\u30ed\u306b\u8fd1\u3065\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u8a08\u753b\u3082\u4e0a\u8a18\u306eSQL\u306f\u304b\u306a\u308a\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3059\u3002<\/p>\n<h2>UNION\u3068CASE\u306e\u9055\u3044<\/h2>\n<p>UNION\u306e\u5834\u5408\u306f\u3001\u6587\u3092\u5358\u4f4d\u306b\u5206\u5c90\u3055\u305b\u3066\u3044\u308b\u306e\u3067\u624b\u7d9a\u7684\u578b\u8a00\u8a9e\u7684\u306a\u767a\u60f3\u306e\u5206\u5c90\u306b\u306a\u308a\u307e\u3059\u3002\u5bfe\u3057\u3066CASE\u306b\u3088\u308b\u5206\u5c90\u306f\u5f0f\u3092\u30d9\u30fc\u30b9\u306b\u3057\u305f\u601d\u8003\u6cd5\u306b\u306a\u308b\u306e\u3067\u3088\u308a\u96c6\u5408\u5fd7\u5411\u578b\u8a00\u8a9e\u3067\u3042\u308bSQL\u306e\u8a00\u8a9e\u7684\u306a\u8003\u3048\u65b9\u306b\u30de\u30c3\u30c1\u3057\u305f\u5b9f\u88c5\u65b9\u6cd5\u306b\u306a\u308a\u307e\u3059\u3002CASE\u3067\u6e08\u3080\u306e\u3067\u3042\u308c\u3070\u3001UNION\u3092\u4f7f\u308f\u305aCASE\u5f0f\u3092\u591a\u7528\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u3063\u305f\u65b9\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>\u624b\u7d9a\u304d\u578b\u8a00\u8a9e\u3067\u8a00\u3046IF\u6587\u3084\u6761\u4ef6\u5206\u5c90\u304c\u51fa\u3066\u304d\u305f\u3089\u307e\u305a\u306fCASE\u5f0f\u3067\u5b9f\u88c5\u3067\u304d\u306a\u3044\u304b\u982d\u3092\u637b\u3089\u305b\u3066\u8003\u3048\u3066\u307f\u308b\u3002<\/li>\n<li>\u51fa\u6765\u308b\u3060\u3051SELECT\u53e5\u306b\u5bc4\u305b\u3089\u308c\u306a\u3044\u304b\u8003\u3048\u3066\u307f\u308b\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u30a2\u30f3\u30c1\u30d1\u30bf\u30fc\u30f3\u4f8b \u4ee5\u4e0b\u306e\u3088\u3046\u306bwhere\u53e5\u306e\u6761\u4ef6\u3060\u3051\u5c11\u3057\u5909\u3048\u3066UNION\u3067\u304f\u3063\u3064\u3051\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308bSQL\u3092\u3084\u3063\u3066\u3057\u307e\u3044\u304c\u3061\u306b\u306a\u308a\u307e\u3059\u3002 SELECT \u5217x\u3001\u5217y as \u5225\u540d FROM \u30c6\u30fc\u30d6\u30eb1 WHERE \u6761\u4ef6A U [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/14843"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14843"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/14843\/revisions"}],"predecessor-version":[{"id":20582,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/14843\/revisions\/20582"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14843"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}