{"id":1670,"date":"2022-08-30T15:18:28","date_gmt":"2022-08-30T15:18:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/how-to-select-a-max-value-from-column-in-query-builder-in-kohana-framework-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:28","modified_gmt":"2022-08-30T15:18:28","slug":"how-to-select-a-max-value-from-column-in-query-builder-in-kohana-framework-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-select-a-max-value-from-column-in-query-builder-in-kohana-framework-collection-of-common-programming-errors\/","title":{"rendered":"How to select a MAX value from column in Query Builder in Kohana framework?-Collection of common programming errors"},"content":{"rendered":"<p>I need to <code>INSERT<\/code> a data to <code>table<\/code>, but before a query I must to know the <code>MAX<\/code> value from column <code>position<\/code>, than to <code>INSERT<\/code> a data WHERE my <code>SELECTED before position+1<\/code>. Is it possible with query builder?<\/p>\n<p>following my first comment I did query:<\/p>\n<pre><code>$p = DB::select(array(DB::expr('MAX(`position`)', 'p')))-&gt;from('supercategories')-&gt;execute();\n\necho $p;\n<\/code><\/pre>\n<p>the error:<\/p>\n<pre><code>ErrorException [ Notice ]: Undefined offset: 1\n\nMODPATH\\database\\classes\\kohana\\database.php [ 505 ]\n\n500      *\/\n501     public function quote_column($column)\n502     {\n503         if (is_array($column))\n504         {\n505             list($column, $alias) = $column;\n506         }\n507 \n508         if ($column instanceof Database_Query)\n509         {\n510             \/\/ Create a sub-query\n<\/code><\/pre>\n<p><strong>upd<\/strong><\/p>\n<pre><code>$p = DB::select(array(DB::expr('MAX(position)'), 'p'))-&gt;from('supercategories')-&gt;execute();\n<\/code><\/pre>\n<p>I used this. But how to get a result?<\/p>\n<p><strong>Solution<\/strong><\/p>\n<pre><code>$p = DB::select(array(DB::expr('MAX(position)'), 'p'))-&gt;from('supercategories')-&gt;execute()-&gt;get('p');\n<\/code><\/pre>\n<ol>\n<li>\n<p>The total solution would look like:<\/p>\n<ol>\n<li><code>DB::select(array(DB::expr('MAX(position)'), 'p'))<\/code><\/li>\n<li>To retrieve the value use <code>get('p')<\/code> or the <code>$result-&gt;p<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:26:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I need to INSERT a data to table, but before a query I must to know the MAX value from column position, than to INSERT a data WHERE my SELECTED before position+1. Is it possible with query builder? following my first comment I did query: $p = DB::select(array(DB::expr(&#8216;MAX(`position`)&#8217;, &#8216;p&#8217;)))-&gt;from(&#8216;supercategories&#8217;)-&gt;execute(); echo $p; the error: ErrorException [ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1670","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1670"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1670\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}