【问题标题】:How to use FIND_IN_SET in codeigniter query?如何在 codeigniter 查询中使用 FIND_IN_SET?
【发布时间】:2017-10-01 06:08:06
【问题描述】:
$array = array('classesID' => 6);
$this->db->select()->from($this->_table_name)->where($array)->order_by($this->_order_by);
$query = $this->db->get();
return $query->result();

我需要获取 classesID 为 6 的行。有些行包含 6、5、4。 所以我需要使用 FIND_IN_SET 查询来检索 classesID 为 6 的位置。

请指导我

谢谢

【问题讨论】:

  • 你是说'classesID'列的值可能是“4,5,6”?

标签: php mysql codeigniter


【解决方案1】:

我需要获取学生 ID 为“4488”和消息 ID 为“1418”的行

$id = 1418;
$student_id = 4488;
    this->db->select('id, action_status, updated_on, student_ids');
    $this->db->where('message_id', $id);
    $this->db->where('find_in_set("'.$student_id.'", student_ids) <> 0');
    $this->db->from('actions_history');
    $query = $this->db->get();

它会返回一个类似

的查询
SELECT id, action_status, updated_on, student_ids FROM actions_history  WHERE message_id = '1418' AND find_in_set("4488", student_ids) <>0

【讨论】:

    【解决方案2】:

    试试这个

    $array = array('classesID' => '5,6,7');
    $this->db->select();
    $this->db->from($this->_table_name);
    $this->db->where("FIND_IN_SET('classesID',".$array['classesID'].")",null,false);
    $this->db->order_by($this->_order_by);
    $query = $this->db->get();
    return $query->result();
    

    【讨论】:

    • 语法错误应该是 FIND_IN_SET(' ".$array['classesID']." ' , 'classesID'),首先是要查找的字符串,然后在第二个参数中查找要查找的列进入,不是吗?
    【解决方案3】:

    你可以这样写查询。

    return $this->db->where('classesID' , '6')->order_by('column_name','desc')->get('table_name')->result_array();
    

    如果需要任何帮助评论

    【讨论】:

    • 谢谢兄弟。我使用了这个查询 $this->db->select()->from(student)->where('FIND_IN_SET(6, classesID)');
    • 但我不知道如何将 id 从控制器传递给模型?
    【解决方案4】:

    对于那些正在寻找基于模型 (CI v4.x) 的解决方案的人,

    $modal->where("FIND_IN_SET('<string to find>', '<column name>')", null, false)->findAll();
    

    在你的情况下,

    $modal->where("FIND_IN_SET('".$array['classesID']."', 'classesID')", null, false)->findAll();
    

    【讨论】:

      猜你喜欢
      • 2017-08-25
      • 2019-05-20
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      相关资源
      最近更新 更多