【发布时间】:2014-01-28 19:20:50
【问题描述】:
我有以下活动记录查询:
$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
$this->db->or_like('email', $search);
$this->db->where_in('id', $ids);
这是一个具有数组 $ids 的函数,其中包含我朋友的 id。我想搜索与我的“喜欢”查询匹配的朋友,但在 $ids 变量中只有一个 id。
我很确定我需要结合 where_in 和所有类似的语句,所以它类似于 (WHERE_IN $ids && Like 语句)。
我不擅长 mysql,所以这里的任何帮助将不胜感激。
谢谢!
function narrow_connections($search) {
//First get all this users connections...
$connections = $this->get_connections($this->session->userdata('user_id'), 0, 0);
if(empty($connections)) {
return array();
}else {
//Need to get an array of id's
$ids = array();
foreach($connections as $con) {
array_push($ids, $con['id']);
}
//Now that we have an array of ID's, find all users that have one of the ids (our connections), AND match a search term to narrow down
//the results.
$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
$this->db->or_like('email', $search);
$this->db->where_in('id', $ids);
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'email' => $row->email,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'current_location_state' => $row->current_location_state,
'current_location' => $row->current_location,
'avatar' => $row->avatar,
'avatar_fb' => $row->avatar_fb,
);
}
return $data;
}
}
【问题讨论】:
-
在 codeigniter 中,使用旧的原始 sql 而不是
active record更容易编写这样的查询
标签: php mysql codeigniter activerecord where-in