【问题标题】:Query to select multiple records from same column with CodeIgniter使用 CodeIgniter 查询从同一列中选择多条记录
【发布时间】:2015-05-06 15:22:35
【问题描述】:
我正在尝试从同一列获取记录。我想从两个 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 吗?
【问题讨论】:
标签:
php
mysql
database
codeigniter
【解决方案1】:
您需要声明从哪里获取“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;
}
}
【解决方案2】:
正如anant所说,你应该使用:
$this->db->where('fa_id', $by);
$this->db->or_where('fa_id', $from);
【解决方案3】:
您可以使用此查询从同一列中选择多条记录
$value = array($by, $from);
$this->db->where_in('fa_id', $value);