两个表定义如下:
---------- Table1 -----------
id type
1 A
2 B
3 C
---------- Table2 ----------
id type sub_type
1 A sub_type1
2 B sub_type1
3 C sub_type2
4 A sub_type3
5 B sub_type2
我想查找所有包含子类型sub_type1
或sub_type3
的类型,我的代码如下:
$SubCategoryFilterArray = ['sub_type1','sub_type3'].
$query = $db->table1();
function SubCategoryFilteredQuery($SubCategoryFilterArray , $query){
$filterSubArrayQuery = '';
for($i = 0 ; $i < sizeof($SubCategoryFilterArray); $i++){
if($i>0)
$filterSubArrayQuery .= ' OR ' ;
$filterSubArrayQuery .= $db->table2()->where(' sub_type = "' . $SubCategoryFilterArray[$i].'" ')->select('type');
}
// $q = $db->table2()->where($p)->select('type');
return $query->where('type', $filterSubArrayQuery); // doubt
}
$finalQuery = SubCategoryFilteredQuery($SubCategoryFilterArray , $query);
echo count($finalQuery);