简体   繁体   中英

Ignore conditions in WHERE clause if empty

I want to show data filtered by WHERE clause. I have applied some variables in place of condition values, Now how to get the data if no condition is applied. I have tried but it. it is working, if the values are given but it is not working if the values are not given.

What should it do?

It has 3 filters. If the date is selected then it should show data by given date. Again if the source is selected then it should show data by the given date and the given source. like this, it should work.

$date_from = NULL;  //the value will be given if the user selects the date range
$date_to = NULL;

$src_id = NULL;  // this value will be given if the user wants data from specific source
$gndr_id = NULL; // if user want data by gender

$leadSql = "SELECT 
            lead.id as lead_id
          , lead.name as lead_name
          , lead.phone as lead_phone
          , lead.email as lead_email 
          , gender.name as lead_gender
          , treatment.name as treatment_name
          , lead.date as lead_date
          , source.name  as  source_name
          , status.name  as status_name
          , lead.remark as lead_remark
        FROM lead 
        join treatment on treatment.id = lead.treatment_id
        join gender on gender.id = lead.gender_id
        join source on source.id = lead.source_id
        join status on status.id = lead.status_id

        where (lead.date BETWEEN $date_from and $date_to) and 
        (lead.source_id = $src_id ) and (lead.gender_id = $gndr_id)

        ORDER BY lead_date DESC";

Please suggest me some way to do it.

As far as my knowledge stretches you need to include the whole WHERE line in a condictional statement to say if values exist apply the WHERE clause if not ignore it.

 if($date_from != NULL && $date_to != NULL .....){
     $where_clause = "where (lead.date BETWEEN $date_from and $date_to) and 
            (lead.source_id = $src_id ) and (lead.gender_id = $gndr_id)";
 }  else{
     $where_clause ="";
 }

$leadSql = "SELECT 
                lead.id as lead_id
              , lead.name as lead_name
              , lead.phone as lead_phone
              , lead.email as lead_email 
              , gender.name as lead_gender
              , treatment.name as treatment_name
              , lead.date as lead_date
              , source.name  as  source_name
              , status.name  as status_name
              , lead.remark as lead_remark
            FROM lead 
            join treatment on treatment.id = lead.treatment_id
            join gender on gender.id = lead.gender_id
            join source on source.id = lead.source_id
            join status on status.id = lead.status_id

            ".$where_clause."

            ORDER BY lead_date DESC";

you can adjust the if/else according to your needs. This is the simplest solution. You can write a conditional statement for each variable and concatenate them together at the end.

$where = [];
$where_clause = '';

if ($date_from && $date_to) {
    $where[] = "(lead.date BETWEEN $date_from and $date_to)";
}

if ($src_id) {
    $where[] = "(lead.source_id = $src_id )";
}

if ($gndr_id) {
    $where[] = "(lead.gender_id = $gndr_id)";
}

if (count($where)) {
    $where_clause = 'WHERE ' . implode(' AND ', $where);
}

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