{"id":4790,"date":"2018-06-21T16:11:54","date_gmt":"2018-06-21T07:11:54","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=4790"},"modified":"2023-05-27T23:18:16","modified_gmt":"2023-05-27T14:18:16","slug":"%e3%80%90sql%e3%80%91%e3%80%8cgroup-by%e5%8f%a5%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=4790","title":{"rendered":"\u3010SQL\u3011\u300cGROUP BY\u53e5\u300d\u3001\u300cHAVING\u53e5\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>\u300cGROUP BY\u53e5\u300d\u3068\u306f\uff1f<\/h2>\n<p>\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u3001\u5546\u54c1\u5206\u985e\u3054\u3068\u306b\u4fa1\u683c\u3092\u96c6\u8a08\u3059\u308b\u7b49\u3001\u30ab\u30c6\u30b4\u30ea\u5206\u3051\u3057\u307e\u3059\u3002<\/p>\n<p>\u96c6\u7d04\u95a2\u6570\uff08COUNT\u3001MAX\u7b49\uff09\u3068\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30ab\u30c6\u30b4\u30ea\u5225\u306b\u96c6\u8a08\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u300cHAVING\u53e5\u300d\u3068\u306f\uff1f<\/h2>\n<p>\u300cGROUP BY\u53e5\u300d\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u7d50\u679c\u306b\u5bfe\u3059\u308b\u6761\u4ef6\u6307\u5b9a\u3092\u3059\u308b\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u300cWHERE\u53e5\u300d\u306f\u3042\u304f\u307e\u3067\u3001<span style=\"color: #ff0000;\"><strong>\u884c\u306b\u5bfe\u3059\u308b\u6761\u4ef6\u6307\u5b9a\u306e\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b\u306e\u3067\u3042\u308a\u3001\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u7d50\u679c\u306b\u5bfe\u3059\u308b\u6761\u4ef6\u6307\u5b9a\u3092\u3059\u308b\u3082\u306e\u3067\u306f\u306a\u3044\u306e\u3067\u6ce8\u610f<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h2>\u300cGROUP BY\u53e5\u300d\u3001\u300cHAVING\u53e5\u300d\u306e\u8a18\u8ff0\u30eb\u30fc\u30eb<\/h2>\n<h3>\u69cb\u6587<\/h3>\n<pre class=\"lang:default decode:true\">SELECT \u52171,\u52172,\u52173\r\n  FROM \u30c6\u30fc\u30d6\u30eb\u540d\r\n  WHERE \u62bd\u51fa\u6761\u4ef6\r\n  GROUP BY \u52171,\u52172,\u52173\r\n  HAVING \u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u305f\u7d50\u679c\u306b\u5bfe\u3059\u308b\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u3002<\/pre>\n<p>GROUP BY\u53e5\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u5fc5\u305aFROM\u53e5\u3084WHERE\u53e5\u306e\u5f8c\u308d\u306b\u7f6e\u304f\u3053\u3068\u3002<\/strong><\/span><\/p>\n<h3>WHERE\u53e5\u3092\u4f7f\u3046\u5834\u5408<\/h3>\n<p>WHERE\u53e5\u3067\u7d5e\u308a\u8fbc\u307e\u308c\u305f\u6761\u4ef6\u306b\u5bfe\u3057\u3066\u3001GROUP BY\u53e5\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>\u300cGROUP BY\u53e5\u300d\u3068\u300c\u96c6\u7d04\u95a2\u6570\u300d\u3092\u4f7f\u3063\u305f\u5834\u5408\u306e\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li>SELECT\u53e5\u306b\u66f8\u3051\u308b\u3082\u306e\u304c\u9650\u5b9a\u3055\u308c\u308b\u3002<\/li>\n<li>GROUP BY\u53e5\u306b\u306f\u3001SELECT\u53e5\u3067\u3064\u3051\u305f\u5225\u540d\u306f\u4f7f\u3048\u306a\u3044\u3002<\/li>\n<li>GROUP BY\u53e5\u306f\u3001\u96c6\u8a08\u7d50\u679c\u306e\u30bd\u30fc\u30c8\u3092\u3057\u306a\u3044\u3002<\/li>\n<li>WHERE\u53e5\u306b\u96c6\u7d04\u95a2\u6570\u3092\u66f8\u304f\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3002<\/li>\n<\/ul>\n<h3>SELECT\u53e5\u306b\u66f8\u3051\u308b\u3082\u306e\u304c\u9650\u5b9a\u3055\u308c\u307e\u3059\u3002<\/h3>\n<p>GROUP BY\u3092\u4f7f\u3063\u305f\u5834\u5408\u306f\u3001SELECT\u53e5\u306b\u66f8\u3051\u308b\u3082\u306e\u306f\u3001\u4e0b\u8a18\u306e3\u3064\u306b\u9650\u5b9a\u3055\u308c\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u5b9a\u6570<\/li>\n<li>\u96c6\u7d04\u95a2\u6570<\/li>\n<li>GROUP BY\u53e5\u3067\u6307\u5b9a\u3057\u305f\u5217\u540d\uff08\u96c6\u7d04\u306e\u30ad\u30fc\u3068\u306a\u308b\u5217\uff09<\/li>\n<\/ul>\n<p>\u8981\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u300c\u96c6\u7d04\u30ad\u30fc\u306b\u767b\u5834\u3057\u3066\u3044\u306a\u3044\u5217\u306f\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u300d<\/strong><\/span>\u306e\u3067\u3059\u3002<\/p>\n<p>\u7406\u7531\u3068\u3057\u3066\u306f\u3001\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30b0\u30eb\u30fc\u30d7\u5316\u306e\u30ad\u30fc\u9805\u76ee\u30671\u610f\u306b\u306a\u3063\u3066\u304a\u308a\u3001\u305d\u308c\u4ee5\u5916\u306e\u9805\u76ee\u306f\u4e00\u3064\u306b\u7279\u5b9a\u3067\u304d\u308b\u5185\u5bb9\u304b\u308f\u304b\u3089\u306a\u3044\u305f\u3081<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>GROUP BY\u53e5\u306b\u306f\u3001SELECT\u53e5\u3067\u3064\u3051\u305f\u5225\u540d\u306f\u4f7f\u3048\u306a\u3044\u3002<\/h3>\n<p>GROUP BY\u53e5\u3067\u306f\u3001SELECT\u53e5\u3067\u4f7f\u3063\u305f\u5225\u540d\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001<span style=\"color: #ff0000;\"><strong>SQL\u306e\u8a55\u4fa1\u9806\u3068\u3057\u3066\u3001SELECT\u53e5\u3088\u308a\u3082\u3001GROUP BY\u53e5\u306e\u65b9\u304c\u5148<\/strong><\/span>\u3060\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>SQL\u306e\u53e5\u306e\u8a55\u4fa1\u9806\u306b\u3064\u3044\u3066\u306f\u3001\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u3082\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"nUzHNQypv6\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=4718\">\u3010SQL\u3011SELECT\u6587\u306e\u8a55\u4fa1\u9806<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.code-magagine.com\/?p=4718&#038;embed=true#?secret=nUzHNQypv6\" data-secret=\"nUzHNQypv6\" width=\"500\" height=\"282\" title=\"&#8220;\u3010SQL\u3011SELECT\u6587\u306e\u8a55\u4fa1\u9806&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<h3>GROUP BY\u53e5\u306e\u96c6\u8a08\u7d50\u679c\u306e\u30bd\u30fc\u30c8\u9806\u306f\u30e9\u30f3\u30c0\u30e0\u306b\u306a\u308b\u3002<\/h3>\n<p>GROUP BY\u53e5\u306e\u96c6\u8a08\u7d50\u679c\u306e\u30bd\u30fc\u30c8\u9806\u306f<span style=\"color: #ff0000;\"><strong>\u30e9\u30f3\u30c0\u30e0<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30c8\u3055\u305b\u308b\u306b\u306f\u3001<span style=\"color: #ff0000;\"><strong>ORDER BY\u53e5<\/strong><\/span>\u3067\u3057\u3063\u304b\u308a\u3068\u30bd\u30fc\u30c8\u3055\u305b\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>WHERE\u53e5\u306b\u96c6\u8a08\u95a2\u6570\u3092\u6307\u5b9a\u3067\u304d\u306a\u3044\u3002<\/h3>\n<p>\u96c6\u8a08\u95a2\u6570\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u306f\u3001\u4e0b\u8a18\u306e\u4e09\u3064\u3067\u3059\u3002<\/p>\n<ul>\n<li>SELECT\u53e5<\/li>\n<li>HAVING\u53e5<\/li>\n<li>ORDER BY\u53e5<\/li>\n<\/ul>\n<h2>\u300cDISTINCT\u30ad\u30fc\u30ef\u30fc\u30c9\u300d\u3068\u3001\u300cGROUP BY\u53e5\u300d\u306e\u6bd4\u8f03<\/h2>\n<p>\u5358\u7d14\u306b\u300cGROUP BY\u53e5\u300d\u3067\u3001\u5217\u3092\u307e\u3068\u3081\u308b\u3060\u3051\u3067\u3042\u308c\u3070<span style=\"color: #ff0000;\"><strong>\u300cDISTINCT\u30ad\u30fc\u30ef\u30fc\u30c9\u300d\u3068\u62bd\u51fa\u7d50\u679c\u306f\u540c\u3058<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u5b9f\u884c\u901f\u5ea6\u3082\u307b\u307c\u540c\u3058\u3067\u3059\u304c\u3001\u300cGROUP BY\u53e5\u300d\u306f\u3042\u304f\u307e\u3067\u300c\u30b0\u30eb\u30fc\u30d7\u5206\u3051\u3092\u3059\u308b\u300d\u3068\u3044\u3046\u76ee\u7684\u306e\u5143\u306b\u4f7f\u7528\u3059\u308b\u69cb\u6587\u3067\u3042\u308a\u3001<span style=\"color: #ff0000;\"><strong>\u300c\u91cd\u8907\u3092\u6392\u9664\u3059\u308b\u300d<\/strong><\/span>\u3068\u3044\u3046\u76ee\u7684\u3068\u306f\u5c11\u3057\u305a\u308c\u308b\u306e\u3067\u3001\u91cd\u8907\u3092\u6392\u9664\u3059\u308b\u76ee\u7684\u3067\u3042\u308c\u3070\u3001\u300cGROUP BY\u53e5\u300d\u3067\u306f\u306a\u304f\u3001<span style=\"color: #ff0000;\"><strong>\u300cDISTINCT\u30ad\u30fc\u30ef\u30fc\u30c9\u300d\u3092\u4f7f\u7528\u3059\u308b\u3079\u304d<\/strong><\/span>\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u300cHAVING\u53e5\u300d\u306b\u6307\u5b9a\u3067\u304d\u308b\u6761\u4ef6<\/h2>\n<p>\u4e0b\u8a18\u306e\uff13\u3064\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u5b9a\u6570<\/li>\n<li>\u96c6\u7d04\u95a2\u6570<\/li>\n<li>GROUP BY\u53e5\u3067\u6307\u5b9a\u3057\u305f\u5217\u540d<\/li>\n<\/ul>\n<p>\u4f8b\u3048\u3070\u3001<span style=\"color: #ff0000;\"><strong>\u300cHAVING COUNT(*) = 2\u300d<\/strong><\/span>\u3068\u3044\u3046\u6761\u4ef6\u6307\u5b9a\u3067\u3042\u308c\u3070\u3001\u300cCOUNT(*)\u300d\u304c\u96c6\u7d04\u95a2\u6570\u3067\u3042\u308a\u3001\u300c\uff12\u300d\u304c\u5b9a\u6570\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u300cWHERE\u53e5\u300d\u3068\u300cHAVING\u53e5\u300d\u306e\u4f7f\u3044\u5206\u3051<\/h3>\n<p>\u307e\u305f\u3001\u4f8b\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><span style=\"color: #000000;\"><span style=\"color: #ff0000;\"><strong>GROUP BY\u53e5\u3067\u6307\u5b9a\u3057\u305f\u5217\u540d<\/strong><\/span>\u3082\u62bd\u51fa\u6761\u4ef6\u3068\u3057\u3066\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/span><\/span><\/p>\n<pre class=\"lang:default decode:true\">SELECT NAME\r\nFROM ITEM\r\nGROUP BY NAME\r\nHAVING NAME = '\u5546\u54c11'<\/pre>\n<p>\u3057\u304b\u3057\u3001GROUP BY\u53e5\u306b\u5bfe\u3059\u308b\u62bd\u51fa\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306f\u3001<span style=\"color: #ff0000;\"><strong>WHERE\u53e5\u3067\u6307\u5b9a\u3059\u308b\u3079\u304d<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<p>\u7406\u7531\u3068\u3057\u3066\u306f\u3001\u3042\u304f\u307e\u3067HAVING\u53e5\u306f<span style=\"color: #ff0000;\"><strong>\u300c\u62bd\u51fa\u7d50\u679c\u306b\u5bfe\u3057\u3066\u3001\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u3082\u306e\u300d\u3067\u3042\u308a\u3001\u300c\u96c6\u7d04\u30ad\u30fc\u306b\u5bfe\u3059\u308b\u6761\u4ef6\u306e\u6307\u5b9a\u300d\u306f\u3001\u96c6\u7d04\u524d\u306eWHERE\u53e5\u3067\u306e\u6761\u4ef6\u6307\u5b9a\u3067\u88dc\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001WHERE\u53e5\u306e\u65b9\u304c\u3001HAVING\u53e5\u3088\u308a\u3082\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u826f\u3044\u306e\u3067\u3001\u3082\u3057\u30b0\u30eb\u30fc\u30d7\u5316\u306e\u5217\u306b\u5bfe\u3057\u3066\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306f\u3001WHERE\u53e5\u306b\u6307\u5b9a\u3059\u308b\u3079\u304d\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u300cGROUP BY\u53e5\u300d\u3068\u306f\uff1f \u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u3001\u5546\u54c1\u5206\u985e\u3054\u3068\u306b\u4fa1\u683c\u3092\u96c6\u8a08\u3059\u308b\u7b49\u3001\u30ab\u30c6\u30b4\u30ea\u5206\u3051\u3057\u307e\u3059\u3002 \u96c6\u7d04\u95a2\u6570\uff08COUNT\u3001MAX\u7b49\uff09\u3068\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30ab\u30c6\u30b4\u30ea\u5225\u306b\u96c6\u8a08\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u300cHAVING\u53e5\u300d\u3068\u306f\uff1f [&hellip;]","protected":false},"author":1,"featured_media":4823,"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\/4790"}],"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=4790"}],"version-history":[{"count":18,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/4790\/revisions"}],"predecessor-version":[{"id":20589,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/4790\/revisions\/20589"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/media\/4823"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4790"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}