douyueqing1530 2019-01-15 05: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-15 05: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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?