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条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集