By a “query is null” you apparently mean the PHP variable being null
. This is in no way specific to SQL or SQL unions.
Repeated if
s
So what you can do is this:
$sql_get_any_packages = "";
if (!is_null($state_sql))
$sql_get_any_packages .= " union " . $state_sql;
if (!is_null($city_sql))
$sql_get_any_packages .= " union " . $city_sql;
if (!is_null($category_sql))
$sql_get_any_packages .= " union " . $category_sql;
if (!is_null($sub_category_sql))
$sql_get_any_packages .= " union " . $sub_category_sql;
if (!is_null($package_sql))
$sql_get_any_packages .= " union " . $package_sql;
$sql_get_any_packages = substr($sql_get_any_packages, strlen(" union "));
This simply collects all non-null
parts, including a leading " union "
for each, and removes the initial " union "
at the end. If all queries might be null
simultaneously, then the end result will be FALSE
.
Using an array
As an alternative, you could drop your parts into an array, use array_filter
to drop the null
elements, and then implode
the result using " union "
as glue. The corresponding code might look like this (see also an example on ideone):
$sql_get_any_packages = array(
$state_sql,
$city_sql,
$category_sql,
$sub_category_sql,
$package_sql);
$sql_get_any_packages = array_filter($sql_get_any_packages);
$sql_get_any_packages = implode(" union ", $sql_get_any_packages);
About intersecting
If you want to intersect the results, you don't want to comoute their union. Once you do a union, you no longer have access to its individual parts, so you can't compare them against one another. To achieve intersections, you use joins in SQL. A full example could look like this:
SELECT package_state.pkg_id
FROM package_state, package_city, package_category, package_category, package
WHERE package_state.state_id IN ($dv_state_ids)
AND package_city.city_id IN ($dv_city_ids)
AND package_category.category_id IN ($dv_category_ids)
AND package_category.subcategory_id IN ($dv_sub_category_ids)
AND package.id in ($dv_category_ids)
AND package_state.pkg_id == package_city.pkg_id
AND package_state.pkg_id == package_category.pkg_id
AND package_state.pkg_id == package.pkg_id
You can build a query like this using solutions like the ones I described above, but they will become slightly more complicated. In particular, the table package_state
in the above query plays a somewhat different role than all the other tables: that's the table form which you SELECT
your output value, and thats also the table which occurs on the left hand side of all the
pkg_idcomparisons, where the other tables appear on the right hand side. So you might want to collect several arrays, one of table names and one of
foo IN (bar)` conditions. Then you can use the first element of the array of table names to play that special role.
Beware the SQL injection
Be careful about pasting strings from user requests into your query. They might contain ugly stuff which will corrupt your data. Make sure the input is sane, e.g. by checking that it is only a comma-separated list of integer values. Or use prepared statements, which can isolate you from most of these problems.