使用 CodeIgniter 从同一列查询 select 多条记录
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 吗?
正如anant所说,你应该使用:
$this->db->where('fa_id', $by);
$this->db->or_where('fa_id', $from);
你需要声明你想从哪里得到'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;
}
}
您可以使用此查询select 来自同一列的多条记录
$value = array($by, $from);
$this->db->where_in('fa_id', $value);
我正在尝试从同一列中获取记录。我想从两个 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 吗?
正如anant所说,你应该使用:
$this->db->where('fa_id', $by);
$this->db->or_where('fa_id', $from);
你需要声明你想从哪里得到'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;
}
}
您可以使用此查询select 来自同一列的多条记录
$value = array($by, $from);
$this->db->where_in('fa_id', $value);