使用多个表格进行搜索和过滤

问题描述 投票:0回答:1

我想从多个表中搜索和过滤,并将搜索词作为用户的输入

我有类似的桌子

profiles

id      first_name  last_name   middle_name gender  life_status lds_number  status
1       Gilbert     Joy         Santos      Male    Living      987456      active
2       swati       santos      Anand       Female  Deceased    777888      active
3       Chirag      Satya       Kirad       Male    Living      111222      active

vital
            
id  profile_id  birth_location  death_location  cause_death ethnicity
1   1           Usa                                         Asian
2   1           Canada                          ABCd        American
3   3           India                                       Indian

用户可以输入多个搜索词,如

living
santos

我的输出查询位于上述搜索词的下方

select * from profiles p
left join vital v on v.profile_id = p.id
where 
(first_name like '%living%' or middle_name like '%living%' or gender like '%living%' or life_status like '%living%' or lds_number like '%living%' )
AND
(first_name like '%santos%' or middle_name like '%santos%' or gender like '%santos%' or life_status like '%santos%' or lds_number like '%santos%' )
AND
(birth_location like '%living%' or death_location like '%living%' or cause_death like '%living%' or ethnicity like '%living%')
AND
(birth_location like '%santos%' or death_location like '%santos%' or cause_death like '%santos%' or ethnicity like '%santos%')

但是如果用户输入搜索词为

Asian
ABCd
那么它不会给出记录,因为
cause_death like %ABCd%
ethnicity like %Asian%
条件不匹配

所以我想在主表中写入

and
条件,在所有子表中写入
or
条件,因为有超过10个子表。

我已经使用codeigniter编写了sql查询

$this->db->distinct();
$this->db->select('p.id, p.first_name, p.last_name, p.middle_name, p.gender, p.life_status');
$this->db->from("profiles p");
$this->db->join("vital v", "v.profile_id = p.id", "left");
$this->db->where('p.status', 'active');
if (!empty($data['search-term']) && !empty(array_filter($search_term_arr))) {
    foreach (array_filter($search_term_arr) as $term) {
        $this->db->group_start();
        $this->db->or_like("p.first_name", $term);
        $this->db->or_like("p.last_name", $term);
        $this->db->or_like("p.middle_name", $term);
        $this->db->or_like("p.gender", $term);
        $this->db->or_like("p.life_status", $term);
        $this->db->or_like("p.lds_number", $term);
        $this->db->or_like("v.birth_location", $term);
        $this->db->or_like("v.death_location", $term);
        $this->db->or_like("v.cause_death", $term);
        $this->db->or_like("v.ethnicity", $term);
        $this->db->group_end();
    }
}

但是查询应该是

select * from profiles p
left join vital v on v.profile_id = p.id
where 
(first_name like '%Asian%' or middle_name like '%Asian%' or gender like '%Asian%' or life_status like '%Asian%' or lds_number like '%Asian%' )
AND
(first_name like '%ABCd%' or middle_name like '%ABCd%' or gender like '%ABCd%' or life_status like '%ABCd%' or lds_number like '%ABCd%' )
AND
(
(birth_location like '%Asian%' or death_location like '%Asian%' or cause_death like '%Asian%' or ethnicity like '%Asian%')
OR
(birth_location like '%ABCd%' or death_location like '%ABCd%' or cause_death like '%ABCd%' or ethnicity like '%ABCd%')
)

随着子表的增多,将会出现多个连接。

我也尝试编写存储过程,但我不是编写存储过程的专家。

任何帮助将不胜感激。 谢谢。

php mysql codeigniter stored-procedures
1个回答
0
投票

你能详细说明一下 AND 是什么意思吗?

select * from profiles p
left join vital v on v.profile_id = p.id
where 
(first_name like '%Asian%' or middle_name like '%Asian%' or gender like '%Asian%' or life_status like '%Asian%' or lds_number like '%Asian%' )
AND
(first_name like '%ABCd%' or middle_name like '%ABCd%' or gender like '%ABCd%' or life_status like '%ABCd%' or lds_number like '%ABCd%' )
AND
(
(birth_location like '%Asian%' or death_location like '%Asian%' or cause_death like '%Asian%' or ethnicity like '%Asian%')
OR
(birth_location like '%ABCd%' or death_location like '%ABCd%' or cause_death like '%ABCd%' or ethnicity like '%ABCd%')
)

因为对我来说,您似乎只是在寻找满足同一表格单元格中不同条件的条目。例如,不会有同时匹配“like Asian”和“like ABCd”的表行。

© www.soinside.com 2019 - 2024. All rights reserved.