{"id":721,"date":"2006-11-05T02:06:07","date_gmt":"2006-11-04T17:06:07","guid":{"rendered":"http:\/\/zone.maple4ever.net\/blog\/archives\/721\/"},"modified":"2006-11-05T02:21:11","modified_gmt":"2006-11-04T17:21:11","slug":"%e4%bc%81%e7%94%bb%e5%80%92%e3%82%8c%e3%81%aesql","status":"publish","type":"post","link":"http:\/\/zone.maple4ever.net\/blog\/archives\/721\/","title":{"rendered":"\u4f01\u753b\u5012\u308c\u306eSQL"},"content":{"rendered":"<p>MySQL 4,0 \u7cfb\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3064\u304b\u3048\u3093\u306e\u304b\u3002\u3002 orz<\/p>\n<p>WP \u3067\u307f\u306a\u3055\u3093\u304b\u3089\u3044\u305f\u3060\u3044\u305f\u30b3\u30e1\u30f3\u30c8\/\u30c8\u30e9\u30c3\u30af\u30d0\u30c3\u30af\u96c6\u8a08\u304b\u3089\u3001\u30ea\u30f3\u30af\u96c6\u3092\u3064\u304f\u308b\u30fb\u30fb\u30fb\u3068\u3044\u3046\u30d7\u30e9\u30b0\u30a4\u30f3\u3092\u8003\u3048\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305b\u3063\u304b\u304f\u66f8\u3044\u305f SQL \u304c\u4f01\u753b\u5012\u308c\u3067\u3057\u305f\u3002\u3000\u3068\u307b\u307b\u3002<\/p>\n<p>MySQL 4.1 \u4ee5\u964d\u306e\u304b\u305f\u306f\u3046\u3054\u304f\u3068\u601d\u3046\u306e\u3067\u904a\u3093\u3067\u304f\u3060\u3055\u3044\u3002 \ud83d\ude42<\/p>\n<p><img decoding=\"async\" id=\"image722\" src=\"http:\/\/zone.maple4ever.net\/blog\/wp-content\/uploads\/2006\/11\/screen.png\" alt=\"phpMyAdmin\" \/><\/p>\n<div class=\"hl-surround\" style=\"height:280px;\"><ol class=\"hl-main\" start=1><li>&nbsp;<span class=\"hl-reserved\">SELECT\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; <\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-default\">.<\/span><span class=\"hl-identifier\">author<\/span><span class=\"hl-default\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-default\">&nbsp; &nbsp; <\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-default\">.<\/span><span class=\"hl-identifier\">author_url<\/span><span class=\"hl-default\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-default\">&nbsp; &nbsp; <\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-default\">.<\/span><span class=\"hl-identifier\">hostname<\/span><span class=\"hl-default\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-default\">&nbsp; &nbsp; <\/span><span class=\"hl-var\">MAX<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_count<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">comment_count<\/span><span class=\"hl-default\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-default\">&nbsp; &nbsp; <\/span><span class=\"hl-var\">MAX<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">trackback_count<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">trackback_count<\/span><span class=\"hl-default\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-default\">&nbsp; &nbsp; <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">comment_count<\/span><span class=\"hl-code\">+<\/span><span class=\"hl-identifier\">trackback_count<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">all_count\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\"><\/span><span class=\"hl-reserved\">FROM\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; <\/span><span class=\"hl-brackets\">((<\/span><span class=\"hl-reserved\">SELECT\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">author<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author_url <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">author_url<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-var\">count<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">comment<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">comment_count<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-quotes\">'' <\/span><span class=\"hl-reserved\">as&nbsp; <\/span><span class=\"hl-identifier\">trackback_count<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">SUBSTRING_INDEX<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">REPLACE<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">comment<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author_url<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">http:\/\/<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">''<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">\/<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-number\">1<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">hostname\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">FROM\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">wp_comments <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">comment\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">WHERE\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment_approved<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">1<\/span><span class=\"hl-quotes\">' <\/span><span class=\"hl-reserved\">AND\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment_author_url<\/span><span class=\"hl-code\"> &lt;&gt; <\/span><span class=\"hl-quotes\">'' <\/span><span class=\"hl-reserved\">AND\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment_type<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-quotes\">''\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-quotes\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">GROUP BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">hostname\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">ORDER BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">comment<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_count <\/span><span class=\"hl-reserved\">desc<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">hostname<\/span><span class=\"hl-brackets\">)\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-brackets\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">UNION\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-reserved\">SELECT\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">author<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author_url <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">author_url<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-quotes\">'' <\/span><span class=\"hl-reserved\">as&nbsp; <\/span><span class=\"hl-identifier\">comment_count<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-var\">count<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">trackback_count<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">SUBSTRING_INDEX<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">REPLACE<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author_url<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">http:\/\/<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">''<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">\/<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-number\">1<\/span><span class=\"hl-brackets\">) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">hostname\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">FROM\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">wp_comments <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">trackback\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">WHERE\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_approved<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">1<\/span><span class=\"hl-quotes\">' <\/span><span class=\"hl-reserved\">AND\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_author_url<\/span><span class=\"hl-code\"> &lt;&gt; <\/span><span class=\"hl-quotes\">'' <\/span><span class=\"hl-reserved\">AND\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_type<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">trackback<\/span><span class=\"hl-quotes\">'\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-quotes\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">GROUP BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">hostname\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\">&nbsp; &nbsp; <\/span><span class=\"hl-reserved\">ORDER BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">trackback<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">comment_count <\/span><span class=\"hl-reserved\">desc<\/span><span class=\"hl-code\">,\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-code\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"hl-identifier\">hostname<\/span><span class=\"hl-brackets\">)) <\/span><span class=\"hl-reserved\">as <\/span><span class=\"hl-identifier\">all_comments\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\"><\/span><span class=\"hl-reserved\">GROUP BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; <\/span><span class=\"hl-identifier\">all_comments<\/span><span class=\"hl-default\">.<\/span><span class=\"hl-identifier\">hostname\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-identifier\"><\/span><span class=\"hl-reserved\">ORDER BY\r<\/span><\/li>\n<li>&nbsp;<span class=\"hl-reserved\">&nbsp; &nbsp; <\/span><span class=\"hl-identifier\">all_count <\/span><span class=\"hl-reserved\">desc<\/span><\/li><\/ol><\/div>\n<p>#\u884c\u756a\u53f7\u306f\u524a\u9664\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u5b9f\u306f \u30b3\u30e1\u30f3\u30c8+\u30c8\u30e9\u30c3\u30af\u30d0\u30c3\u30af\u306e\u5408\u8a08\u304c\u30d0\u30b0\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3001\u306a\u3093\u3067\u3060\u308d\u3046\u3002<\/p>\n<p>\u3064\u304f\u308b\u3068\u3057\u305f\u3089 PHP \u306e\u30e1\u30e2\u30ea\u3067\u30b5\u30de\u30eb\u3057\u304b\u306a\u3044\u304b\u306a\uff5e\u3002\u3000\u826f\u3044\u6848\u601d\u3044\u3064\u3044\u305f\u304b\u305f\u306f\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002 \ud83d\ude1b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL 4,0 \u7cfb\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u30fc\u3064\u304b\u3048\u3093\u306e\u304b\u3002\u3002 orz WP \u3067\u307f\u306a\u3055\u3093\u304b\u3089\u3044\u305f\u3060\u3044\u305f\u30b3\u30e1\u30f3\u30c8\/\u30c8\u30e9\u30c3\u30af\u30d0\u30c3\u30af\u96c6\u8a08\u304b\u3089\u3001\u30ea\u30f3\u30af\u96c6\u3092\u3064\u304f\u308b\u30fb\u30fb\u30fb\u3068\u3044\u3046\u30d7\u30e9\u30b0\u30a4\u30f3\u3092\u8003\u3048\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305b\u3063\u304b\u304f\u66f8\u3044\u305f SQL \u304c\u4f01\u753b\u5012\u308c\u3067\u3057\u305f\u3002\u3000\u3068\u307b\u307b\u3002 MySQL 4.1 \u4ee5\u964d\u306e\u304b\u305f\u306f\u3046\u3054\u304f\u3068\u601d\u3046\u306e\u3067\u904a\u3093\u3067\u304f\u3060\u3055\u3044\u3002 \ud83d\ude42 &nbsp;SELECT &nbsp;&nbsp; &nbsp; all_comments.author, &nbsp;&nbsp; &nbsp; all_comments.author_url, &nbsp;&nbsp; &nbsp; all_comments.hostname, &nbsp;&nbsp; &nbsp; MAX(all_comments.comment_count) as comment_count, &nbsp;&nbsp; &nbsp; MAX(all_comments.trackback_count) as trackback_count, &nbsp;&nbsp; &nbsp; (comment_count+trackback_count) as all_count &nbsp;FROM &nbsp;&nbsp; &nbsp; ((SELECT &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; comment.comment_author as author, &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; comment.comment_author_url as author_url, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":0,"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zone.maple4ever.net\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}