douyueqing1530 2019-01-14 21:33
浏览 36
已采纳

如何从codeigniter中的varchar字段获取整数值的max()?

rf_id  rf_name  
12      12
13      13
14      14
15      15
16      GF
17      BASE

I want to get 15 from column rf_name in Codeigniter. each time I should get max('rf_name') where characters not allowed

  • 写回答

2条回答 默认 最新

  • douzhang8144 2019-01-14 21:45
    关注

    First, make sure column rf_name data type should be integer/float, not varchar/string/text etc.

    So, according to the data type of the column, it will not store any string value

    $maxid = $this->db->query('SELECT MAX(rf_name) AS maxid FROM table')->row()->maxid;
    

    OR

    $this->db->select_max('rf_name');
    $query = $this->db->get('table');
    

    If you have both string and numeric values you have to do the following steps

    1. Get all values of column

      $this->db->select('rf_name');
      $this-db->from('table');
      $values = $this->db->get('table')->result_array(); 
      
    2. Get only values from an array

      $only_values= array();
      foreach($values as $value){
         if (is_numeric($value['rf_name'])) {
            array_push($only_values, $value['rf_name']);
         }
      }
      
    3. use max() and pass the array of values to get maximum numeric value from it.

      $max_value = max($only_values); 
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?