简体   繁体   中英

MySQL query with where clause and if statements

I'm stuck again on a MySQL query and I would like some help.

I am trying to find the best possible way to do this. What I want to do is check if $var = 'x' and based on that run a different query.

Part of the code:

if ($var1 == '0' AND $var2 != '0' AND $var3 != '0' AND $var4 != '0') {
    echo $query = "SELECT * FROM table WHERE var1 = ''";
}elseif ($var1 == '1' AND $var2 != '0' AND $var3 != '0' AND $var4 != '0') {
    echo $query = "SELECT * FROM table WHERE var1 = '1'";
}elseif ($var1 != '0' AND $var2 == '0' AND $var3 != '0' AND $var4 != '0') {
    echo $query = "SELECT * FROM table WHERE var2 = ''";
}elseif ($var1 == '0' AND $var2 == '0' AND $var3 != '0' AND $var4 != '0') {
    echo $query = "SELECT * FROM table WHERE var1 = '' AND var2 = ''";
} etc..

The connections are a LOT. Is there a better way to do this? Pretty much each variable (in total 7) got 3 different choices. I can't put var = $var under the WHERE clause because sometimes when $var = '0' I mean all the results so I don't even put it there..

This is a filtering script/code by the way..

Does this even make sense to you? Thanks!

I think this can be done with a single query and varying WHERE conditions. You need to first define when to put var1 = '' in your WHERE clause. You should find out the cases when you need varX = '' in your query one by one.

$conds = array();
if ($var1 == '0' || $var2 == '0') {
  $conds[] = "var1 = ''"; // this should be the only point where var1 is included in your query, so you need to have every possible condition in this if part
}
if ($var3 == '0' && $var4 != '0') {
  $conds[] = "var3 = ''";
}
// ..etc..
$query = "SELECT * FROM TABLE WHERE " . implode(' AND ', $conds);

How about:

$where_parts = array();
if (isset($var1)) {
  if ($var1 == 0) { $var1 = ''; }
  $where_parts [] = "var1 = '" . $var1 . "'";
}
if (isset($var2)) {
if ($var2 == 0) { $var2 = ''; }
  $where_parts [] = "var2 = '" . $var2 . "'";
}
if (isset($var3)) {
if ($var3 == 0) { $var3 = ''; }
  $where_parts [] = "var3 = '" . $var3 . "'";
}
if (isset($var4)) {
if ($var4 == 0) { $var4 = ''; }
  $where_parts [] = "var4 = '" . $var4 . "'";
}

$query = "SELECT * FROM table WHERE " . implode(' AND ', $where_parts );

Is it possible for you to only concentrate on the variables being relevant for the query? For example something like:

$str = "";

# When will "var1" be in the query
switch($var1) {
  case '0' : $str .= " AND $var1=''"; break;
  case '1' : $str .= " AND $var1='1'"; break;
}

# When will "var2" be in the query
switch($var2) {
  case '0' : $str .= " AND $var2=''"; break;
  case '1' : $str .= " AND $var2='1'"; break;
}

...

# If not empty then remove the first " AND "
if(!empty($str)) {
    $str = substr($str,4);
}

# Final query is a lot of concatenated strings
$query = "SELECT * FROM table WHERE $str";

It may not be the most code-optimized but it is easy to understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM