我有 5 个查询,如下所示,我需要对所有查询执行联合
if($dv_state_ids!="")//check if no state is entered
$state_sql="SELECT pkg_id FROM package_state where state_id in ($dv_state_ids) ";
if($dv_city_ids!="")//check if no city is entered
$city_sql="SELECT pkg_id FROM package_city where city_id in ($dv_city_ids)";
if($dv_category_ids!="")//check if nocategory is entered
$category_sql="SELECT pkg_id FROM package_category where category_id in ($dv_category_ids) ";
if($dv_sub_category_ids!="")//check if no subcategory is found
$sub_category_sql="SELECT pkg_id FROM package_category where subcategory_id in ($dv_sub_category_ids) ";
if($dv_category_ids!="")
$package_sql="SELECT pkg_id FROM package where id in ($dv_category_ids) ";
我可以通过以下语句对上述查询运行联合
$sql_get_any_packages=$state_sql." union ".$city_sql."
union
".$category_sql." union ".$sub_category_sql." union ".$package_sql ;
但是如果任何查询为空,那么这将在 mysql 中产生错误。一种方法是我构建 32 个条件(这将由这 5 个查询形成以检查不同的条件)
任何人都可以建议我另一种出路吗
我还需要对这 5 个查询的结果执行交集,并且 sql 不支持 INTERSECT
请在这件事上给予我帮助