{"id":870,"date":"2015-01-24T02:29:00","date_gmt":"2015-01-24T10:29:00","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=870"},"modified":"2015-01-24T02:32:08","modified_gmt":"2015-01-24T10:32:08","slug":"zend_db_select%e3%81%a7%e8%a4%87%e6%95%b0%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%8b%e3%82%89%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e3%81%9f%e3%81%84%e3%81%ae%e3%81%a0","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/870","title":{"rendered":"Zend_Db_Select\u3067\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u306e\u3060\u304c&#8230;"},"content":{"rendered":"<p>Zend Framework\u3067Web\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3053\u3068\u306b\u306a\u308a\u3001\u4e45\u3057\u3076\u308a\u306bZend Framework\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u898b\u306a\u304c\u3089\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u3044\u305f\u306e\u3060\u304c\u3001\u3082\u306e\u3059\u3054\u304f\u5358\u7d14\u306a\u3053\u3068\u306a\u306f\u305a\u306a\u306e\u306b<code>Zend_Db_Select<\/code>\u306e\u4ed5\u69d8\uff08\uff1f\uff09\u306e\u305b\u3044\u3067\u3061\u3087\u3044\u3068\u30cf\u30de\u3063\u3066\u3044\u305f\u3002<\/p>\n<p>Web\u30a2\u30d7\u30ea\u3067\u306f\u304a\u6c7a\u307e\u308a\u306e\u30e6\u30fc\u30b6\u30fc\u7ba1\u7406\u6a5f\u80fd\u306a\u3093\u3060\u304c\u3001\u8a71\u3092\u5358\u7d14\u306b\u3059\u308b\u305f\u3081\u306b\u3084\u308a\u305f\u3044\u3053\u3068\u3060\u3051\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a<code>roles<\/code>\u30c6\u30fc\u30d6\u30eb\u3068<code>users<\/code>\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u3088\u3046\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE roles (\r\n  id SERIAL PRIMARY KEY,\r\n  name VARCHAR(255) NOT NULL UNIQUE\r\n);\r\n\r\nCREATE TABLE users (\r\n  id SERIAL PRIMARY KEY,\r\n  name VARCHAR(255) NOT NULL UNIQUE\r\n  role INT FOREIGN KEY REFERENCE\r\n);\r\n<\/pre>\n<p>\u30e6\u30fc\u30b6\u30fc\u306b\u30ed\u30fc\u30eb\u3092\u5272\u308a\u5f53\u3066\u308b\u5178\u578b\u30d1\u30bf\u30fc\u30f3\u3002<br \/>\n\u3067\u3001\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3001<\/p>\n<p><strong>\u30ed\u30fc\u30eb\u306e\u4e00\u89a7\u8868\u793a\u3092\u3059\u308b\u969b\u306b\u3001\u305d\u306e\u30ed\u30fc\u30eb\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u6570\u3082\u51fa\u3057\u305f\u3044\uff01<\/strong><\/p>\n<p><!--more--><br \/>\n\u3082\u3061\u308d\u3093\u3001SQL\u4e00\u767a\u3067\u7c21\u5358\u306b\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT roles.*,count(users.role) AS users\r\nFROM roles,users\r\nWHERE roles.id = users.role\r\nGROUP BY roles.id;\r\n<\/pre>\n<p>\u3053\u306eSQL\u3092<code>Zend_Db_Select<\/code>\u3067\u5b9f\u88c5\u3057\u3088\u3046\u3068\u601d\u3063\u3066\u3001<code>FROM<\/code>\u53e5\u306b2\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4e26\u3079\u308b\u305f\u3081\u306b<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$select = $db-&gt;select();\r\n$count = new Zend_Db_Expr('count(users.role)');\r\n$select-&gt;from('roles')\r\n       -&gt;from('users', array('roles.*', 'users' =&gt; $count))\r\n       -&gt;where('roles.id = users.role')\r\n       -&gt;group('roles.id');\r\nvar_dump($select-&gt;__toString()); \r\n<\/pre>\n<p>\u3068\u3084\u3063\u3066\u307f\u305f\u304c&#8230;<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT roles.*,count(users.role) AS users\r\nFROM roles INNER JOIN users\r\nWHERE roles.id = users.role\r\nGROUP BY roles.id;\r\n<\/pre>\n<p>\u3068\u30012\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u304c<code>INNER JOIN<\/code>\u3067\u7d50\u5408\u3059\u308b\u3088\u3046\u306aSQL\u6587\u304c\u3067\u304d\u3066\u3057\u307e\u3063\u305f\u3002<\/p>\n<p>SQL\u3092\u5b9f\u884c\u3057\u3066\u3082<\/p>\n<pre>\r\nERROR:  syntax error at or near \"WHERE\"\r\nLINE 3: WHERE roles.id = users.role\r\n        ^\r\n<\/pre>\n<p>\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\uff08\u3061\u306a\u307f\u306bRDB\u306b\u306fPostgreSQL\u3092\u4f7f\u3063\u3066\u3044\u308b\uff09\u3002<\/p>\n<p><code>INNER JOIN<\/code>\u3058\u3083\u306a\u304f\u3066\u30ab\u30f3\u30de\u3067\u30c6\u30fc\u30d6\u30eb\u540d\u3092\u4e26\u3079\u308b\u65b9\u6cd5\u306f\u306a\u3044\u3082\u306e\u304b\u3068<code>Zend_Db_Select<\/code>\u306e\u30b3\u30fc\u30c9\u3082\u8abf\u3079\u3066\u307f\u305f\u304c<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function from($name, $cols = '*', $schema = null)\r\n{    \r\n    return $this-&gt;_join(self::FROM, $name, null, $cols, $schema);\r\n}\r\n<\/pre>\n<p>\u3068\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001<code>JOIN<\/code>\u3059\u308b\u4ee5\u5916\u3001\u9078\u629e\u306e\u4f59\u5730\u304c\u306a\u3044\u3088\u3046\u3067&#8230;\u3002<\/p>\n<p>\u6700\u7d42\u7684\u306b\u306f\u3001\u305d\u308c\u305e\u308c\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u5fdc\u3059\u308b<code>Zend_Db_Table<\/code>\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u3001\u305d\u308c\u305e\u308c\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089<code>fetchAll()<\/code>\u3057\u3066\u3001PHP\u4e0a\u3067\u7d50\u5408\u3055\u305b\u308b\u3068\u3044\u3046\u65b9\u6cd5\u306b\u843d\u3061\u7740\u3044\u305f\u3002SQL\u304c2\u767a\u5b9f\u884c\u3055\u308c\u308b\u306e\u304c\u5acc\u3060\u3051\u3069\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u30b7\u30d3\u30a2\u306b\u306a\u308bWeb\u30a2\u30d7\u30ea\u3067\u3082\u306a\u3044\u306e\u3067\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u91cd\u8996\u3057\u3066\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass RolesController extends Zend_Controller_Action\r\n{\r\n  public function indexAction()\r\n  {\r\n    $table = new DbTable_Roles;\r\n    $roles = array();\r\n    $users = $this-&gt;_getRefUsers();\r\n    foreach ($table-&gt;fetchAll($table-&gt;select()) as $row) {\r\n      $role = (object)$row-&gt;toArray();\r\n      $role-&gt;users = array_key_exists($role-&gt;id, $users) ? $users[$role-&gt;id] : 0;\r\n      $roles[] = $role;\r\n    }\r\n    $this-&gt;view-&gt;roles = $roles;\r\n  }\r\n\r\n  private function _getRefUsers()\r\n  {\r\n    $table = new DbTable_Users;\r\n    $count = new Zend_Db_Expr('count(role)');\r\n    $select = $table-&gt;select()-&gt;from($table, array('role', 'users' =&gt; $count))-&gt;group('role');\r\n    $users = array();\r\n    foreach ($table-&gt;fetchAll($select) as $row) {\r\n      $users[$row-&gt;role] = $row-&gt;users;\r\n    }\r\n    return $users;\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Zend Framework\u3067Web\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3053\u3068\u306b\u306a\u308a\u3001\u4e45\u3057\u3076\u308a\u306bZend Framework\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u898b\u306a\u304c\u3089\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u3044\u305f\u306e\u3060\u304c\u3001\u3082\u306e\u3059\u3054\u304f\u5358\u7d14\u306a\u3053\u3068\u306a\u306f\u305a\u306a\u306e\u306bZend_Db_Select\u306e\u4ed5\u69d8\uff08\uff1f\uff09 <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/870' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[269,268,267,265,266],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/870"}],"collection":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}