gpt4 book ai didi

php - Codeigniter 结合 where_in 和 like 事件记录查询

转载 作者:可可西里 更新时间:2023-11-01 08:33:01 25 4
gpt4 key购买 nike

我有以下事件记录查询:

$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 的函数,其中包含我 friend 的 ID。我想搜索与我的“喜欢”查询相匹配的 friend ,但 $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;

}
}

最佳答案

你想找到所有的 friend 吗?如果只有一个 id,那么你不需要 like 部分,因为你已经找到了你的 friend 。另一方面,如果您不确定 friend 的 id,只想找到所有符合您喜欢条件的 friend ,您可以删除 where_in 部分。

这会找到你的 friend :

$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);

考虑到只有一个id,这样的查询只会找到一个 friend :

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->where_in('id', $ids);

编辑

有时,使用 codeigniter,最好使用原始查询:

$this->db->query('
SELECT `id`, `email`, `first_name`, `last_name`, `current_location_state`, `current_location`, `avatar`, `avatar_fb`
FROM `users`
WHERE (
`first_name` like ?
or `last_name` like ?
or concat(`first_name`, ' ', `last_name`) like ?
or `email` like ?)
AND `id` in('
.join(',',
array_map(function($e) { return (int) $e; }, $ids))
.')',
"%$search%", "%$search%", "%$search%", "$search")->result();

关于php - Codeigniter 结合 where_in 和 like 事件记录查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com