简体   繁体   中英

ignore column if column value is null or empty for searching in mysql table

SELECT logic_id
FROM business_logic_details
WHERE if(form_completion != 0.00,form_completion,0) = '90'
  AND if(query_type IS NOT NULL
         OR query_type !='',query_type,0) LIKE '%domestic%'
  AND if(client_type IS NOT NULL
         OR client_type !='',client_type,0) LIKE '%existing%'
  AND if(tour_package IS NOT NULL
         OR tour_package !='',tour_package,0) LIKE '%S%'
  AND if(tour_type IS NOT NULL
         OR tour_type !='',tour_type,0) LIKE '%2%'
  AND if(currency IS NOT NULL
         OR currency !='',currency,0) = 'INR'
  AND if(country IS NOT NULL
         OR country !='',country,0) = '105'
  AND if(adults IS NOT NULL
         OR adults !='',adults,0) = '1'
  AND if(duration_of_stay IS NOT NULL
         OR duration_of_stay !='',duration_of_stay,0) = '5'
ORDER BY logic_id ASC

Use COALESCE function for that:

SELECT logic_id
FROM business_logic_details
WHERE COALESCE(form_completion, 90) = 90
  AND COALESCE(query_type, 'domestic') LIKE '%domestic%'
        ...

I also strongly suggest to compare integers as integers, and not as strings:

Good: 0 = 90
Bad: 0 = '90'

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