{"id":1670,"date":"2014-10-29T10:00:10","date_gmt":"2014-10-29T01:00:10","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=1670"},"modified":"2014-10-31T11:08:57","modified_gmt":"2014-10-31T02:08:57","slug":"%e6%ad%a3%e8%a6%8f%e5%8c%96%e7%b8%a6%e6%8c%81%e3%81%a1%e3%80%81%e8%a1%8c%e6%8c%81%e3%81%a1%e3%83%87%e3%83%bc%e3%82%bf%e3%81%ab%e5%af%be%e3%81%99%e3%82%8b%e6%a4%9c%e7%b4%a2%e3%81%ae%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/%e6%ad%a3%e8%a6%8f%e5%8c%96%e7%b8%a6%e6%8c%81%e3%81%a1%e3%80%81%e8%a1%8c%e6%8c%81%e3%81%a1%e3%83%87%e3%83%bc%e3%82%bf%e3%81%ab%e5%af%be%e3%81%99%e3%82%8b%e6%a4%9c%e7%b4%a2%e3%81%ae%e5%ae%9f%e8%a3%85\/","title":{"rendered":"\u6b63\u898f\u5316(\u7e26\u6301\u3061\u3001\u884c\u6301\u3061)\u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u691c\u7d22\u306e\u5b9f\u88c5(SQL\u7de8)"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30a2\u30eb\u30d0\u30a4\u30c8\u3067Symfony2\u3092\u4f7f\u3063\u3066\u793e\u5185\u30b7\u30b9\u30c6\u30e0\u306e\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u95a2\u3059\u308b\u304a\u8a71\u3067\u3059\u3002<\/p>\n<p>MySQL\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30c6\u30fc\u30d6\u30eb test_option<\/h3>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nCREATE TABLE IF NOT EXISTS test_option (\r\n  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(255) NOT NULL,\r\n  option_1 varchar(255) NOT NULL,\r\n  option_2 varchar(255) NOT NULL,\r\n  option_3 varchar(255) NOT NULL,\r\n  UNIQUE KEY id (id)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\n<\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\">id<\/th>\n<th align=\"center\">name<\/th>\n<th align=\"center\">option_1<\/th>\n<th align=\"center\">option_2<\/th>\n<th align=\"center\">option_3<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">taro<\/td>\n<td align=\"center\">good<\/td>\n<td align=\"center\">bad<\/td>\n<td align=\"center\">bad<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">jiro<\/td>\n<td align=\"center\">bad<\/td>\n<td align=\"center\">good<\/td>\n<td align=\"center\">good<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u3053\u306e\u3088\u3046\u306a\u6a2a\u6301\u3061\u30c6\u30fc\u30d6\u30eb\u306a\u3089\u3070\u3001option_1\u304cgood\u304b\u3064option_2\u304cbad\u306e\u4eba\u3092\u62bd\u51fa\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306eSQL\u6587\u3092\u767a\u884c\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nSELECT name FROM test_option\r\nWHERE\r\noption_1 = &quot;good&quot;\r\nAND\r\noption_2 = &quot;bad&quot;\r\n\r\n<\/pre>\n<p>\u7d50\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">taro<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u3053\u3061\u3089\u306b\u95a2\u3057\u3066\u306f\u7279\u306b\u96e3\u3057\u3044\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u3001\u7279\u306b\u89e3\u8aac\u3092\u3057\u307e\u305b\u3093\u3002<\/p>\n<p>\u7d9a\u3044\u3066\u3001\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u95a2\u3057\u3066\u3001<\/p>\n<ul>\n<li>\n\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u6570\u304c\u53ef\u5909\u3067\u3042\u308b\n<\/li>\n<li>\n\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u5bfe\u3057\u3066Value\u304c\u5165\u3089\u306a\u3044\u3053\u3068\u304c\u3042\u308b\n<\/li>\n<li>\n\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u6570\u304c\u81a8\u5927\u3067\u3042\u308b\n<\/li>\n<\/ul>\n<p>\u7b49\u306e\u4e8b\u614b\u304c\u767a\u751f\u3057\u3001\u30c7\u30fc\u30bf\u306e\u6a2a\u6301\u3061\u304c\u5408\u7406\u7684\u3067\u306f\u306a\u304f\u306a\u308a\u3001\u7e26\u6301\u3061\u3092\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3068\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30c6\u30fc\u30d6\u30eb test_name<\/h3>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nCREATE TABLE IF NOT EXISTS test_name (\r\n  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(255) NOT NULL,\r\n  UNIQUE KEY id (id)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\n<\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\">id<\/th>\n<th align=\"center\">name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">taro<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">jiro<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u30c6\u30fc\u30d6\u30eb test_option_2<\/h3>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nCREATE TABLE IF NOT EXISTS `test_option_2` (\r\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name_id` int(11) NOT NULL,\r\n  `option_id` int(11) NOT NULL,\r\n  `value` varchar(255) NOT NULL,\r\n  UNIQUE KEY `id` (`id`),\r\n  KEY `name_id` (`name_id`,`option_id`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\n<\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\">id<\/th>\n<th align=\"center\">name_id<\/th>\n<th align=\"center\">option_id<\/th>\n<th align=\"center\">value<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">good<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">2<\/td>\n<td align=\"center\">bad<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">3<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">3<\/td>\n<td align=\"center\">bad<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4<\/td>\n<td align=\"center\">2<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">bad<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">5<\/td>\n<td align=\"center\">2<\/td>\n<td align=\"center\">2<\/td>\n<td align=\"center\">good<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">6<\/td>\n<td align=\"center\">2<\/td>\n<td align=\"center\">3<\/td>\n<td align=\"center\">good<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u73fe\u5b9f\u306b\u306f\u3001\u3055\u3089\u306b\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u540d\u524d\u7528\u306e\u30de\u30b9\u30bf\u30fc\u30c6\u30fc\u30d6\u30eb\u304c\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u304c\u3001\u3053\u3053\u3067\u306f\u7701\u7565\u3057\u307e\u3059\u3002<\/p>\n<p>\u3055\u3066\u3001\u3053\u3053\u3067option_1(option_id = 1)\u304cgood\u304b\u3064option_2(option_id = 2)\u304cbad\u3067\u3042\u308b\u3088\u3046\u306a\u4eba\u3092\u62bd\u51fa\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u3053\u3053\u3067\u5148\u307b\u3069\u306eSQL\u6587\u3092\u305d\u306e\u307e\u307e\u9069\u7528\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306eSQL\u6587\u3092<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nSELECT * FROM test_option_2\r\nWHERE \r\noption_id = 1\r\nAND \r\nvalue = &quot;good&quot;\r\nAND\r\noption_id = 2\r\nAND \r\nvalue = &quot;bad&quot;\r\n\r\n<\/pre>\n<p>\u3092\u767a\u884c\u3059\u308b\u3068\u3001\u5f53\u305f\u308a\u524d\u3067\u3059\u304c\u3001\u7d50\u679c\u30bb\u30c3\u30c8\u306f\u7a7a\u306b\u306a\u308a\u307e\u3059\u3002\u3061\u306a\u307f\u306bOR\u691c\u7d22\u306e\u5834\u5408\u306f\u3046\u307e\u304f\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306aSQL\u6587\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT opt_1.* FROM test_option_2 opt_1\r\nLEFT JOIN test_option_2 opt_2\r\nON opt_1.name_id = opt_2.name_id\r\nWHERE \r\n(\r\n    opt_1.option_id = 1\r\n    AND \r\n    opt_1.value = &quot;good&quot;\r\n)\r\nAND\r\n(\r\n    opt_2.option_id = 2\r\n    AND \r\n    opt_2.value = &quot;bad&quot;\r\n)\r\n<\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\">id<\/th>\n<th align=\"center\">name_id<\/th>\n<th align=\"center\">option_id<\/th>\n<th align=\"center\">value<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">1<\/td>\n<td align=\"center\">good<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u76ee\u7684\u306e\u7d50\u679c\u3067\u3059\u306d\u3002\u5f8c\u306f\u3053\u306e\u7d50\u679c\u306b\u5bfe\u3057\u3001test_name\u304b\u3089JOIN\u3092\u304a\u3053\u306a\u3044\u307e\u3057\u3087\u3046\u3002\u3082\u3061\u308d\u3093OR\u691c\u7d22\u306b\u95a2\u3057\u3066\u3082\u3046\u307e\u304f\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u30dd\u30a4\u30f3\u30c8\u306f\u3001\u7e26\u6301\u3061\u304b\u3089\u6a2a\u6301\u3061\u3078\u306e\u5909\u63db\u3092\u304a\u3053\u306a\u3044\u691c\u7d22\u3092\u304b\u3051\u3066\u3044\u308b\u70b9\u3067\u3059\u3002<\/p>\n<p>\u7e26\u6301\u3061\u304b\u3089\u6a2a\u6301\u3061\u3078\u306e\u5909\u63db\u306f\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u304c\u53c2\u8003\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/gist.github.com\/aamine\/5565025\" title=\"https:\/\/gist.github.com\/aamine\/5565025\" target=\"_blank\">https:\/\/gist.github.com\/aamine\/5565025<\/a><\/p>\n<p>\u4e0a\u8a18\u30da\u30fc\u30b8\u3067\u306f\u3001\u5358\u4e00\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u96c6\u8a08\u95a2\u6570\u3092\u7528\u3044\u3066\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u3061\u3089\u3067\u306f\u3001JOIN\u3092\u5229\u7528\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001\u96c6\u8a08\u95a2\u6570\u3092\u7528\u3044\u3066\u3044\u307e\u305b\u3093\u3002\u307e\u305f\u3001\u691c\u7d22\u5185\u3067WHERE\u6587\u306b\u304a\u3044\u3066\u4e00\u6c17\u306b\u7d5e\u3089\u308c\u308b\u3066\u3081\u3001\u901f\u5ea6\u7684\u306b\u3082\u554f\u984c\u306f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u4ee5\u4e0a\u3067\u3059\u3001\u6b21\u56de\u306f\u672c\u691c\u7d22\u3092PHP\u3067\u5b9f\u88c5\u3059\u308b\u65b9\u6cd5\u3092\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30a2\u30eb\u30d0\u30a4\u30c8\u3067Symfony2\u3092\u4f7f\u3063\u3066\u793e\u5185\u30b7\u30b9\u30c6\u30e0\u306e\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u3082\u306e\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u95a2\u3059\u308b\u304a\u8a71\u3067\u3059\u3002 MySQL\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u30c6\u30fc\u30d6\u30eb test_option C&#8230;<\/p>\n","protected":false},"author":34,"featured_media":374,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"","_original_post":"","footnotes":""},"categories":[24,23],"tags":[],"class_list":{"0":"post-1670","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-mysql","8":"category-23","9":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=1670"}],"version-history":[{"count":12,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1670\/revisions"}],"predecessor-version":[{"id":1768,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1670\/revisions\/1768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/374"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=1670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=1670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=1670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}