查询以使用CodeIgniter从同一列中选择多条记录


Query to select multiple records from same column with CodeIgniter

我正试图从同一列中获取记录。我想从两个ID中获得农场代码,我尝试在CodeIgniter文档的帮助下遵循,但它不起作用。

        $this->db->select('fa_code');
        $this->db->where('fa_id', $by);
        $this->db->where('fa_id', $from);
        $res=$this->db->get('tukai_farms')->result();

怎么了?我们不是在同一字段中使用AND吗?

您需要从想要获得"fa_code"的位置声明,然后声明您拥有的条件:

这是正确的方式:

public function selectFaCode(){
        $by = 1;
        $from = 2;
        $query = $this->db->select('fa_code')
                            ->from('tukai_farms')
                            ->where('fa_id', $by)
                            ->or_where('fa_id', $from) //Remember that you are declaring the same condition ! Remove this to keep the first where or change the column name if you want to get items from table where the condition is another column.
                            ->get()
                            ->result();
        foreach($query as $row){
            echo $row->fa_code;
        }
    }

正如anant所说,您应该使用:

$this->db->where('fa_id', $by);
$this->db->or_where('fa_id', $from);

您可以使用此查询从同一列中选择多条记录

$value = array($by, $from);
$this->db->where_in('fa_id', $value);