SQL statement filter result

后端 未结 2 1252
长情又很酷
长情又很酷 2021-01-26 04:04

I need to filter the SQL query result according to 3 conditions:
1. Location
2. Doctor Name
3. Specialty Name

Below is the sql query if all 3 conditions are

2条回答
  •  悲&欢浪女
    2021-01-26 04:26

    I think it would be better to do this in the code than sql as you have done. Not really any way around it since the queries are so different, but a terse way to do it would be:

    $loc_where = empty($loc) ? 'Location IS NOT NULL' : "Location = $loc";
    $doc_where = empty($doc) ? 'AND DoctorName IS NOT NULL' : "AND DoctorName = $doc";
    $spec_where = empty($spec) ? 'AND SpecialtyName IS NOT NULL' : "AND SpecialtyName = $spec";
    
    query ... WHERE $loc_where $doc_where $spec_where
    

提交回复
热议问题