简体   繁体   English

如果列值是null或为空以在mysql表中搜索,则忽略列

[英]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: 为此使用COALESCE函数:

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 好:0 = 90
Bad: 0 = '90' 错误:0 =“ 90”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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