0
 SELECT    ( CASE 
                        WHEN 1944.0000 >= country_from_price 
                            AND 1944.0000 <= country_to_price
                        THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
                            ELSE 1944.0000
                        END 
                        )   
                       FROM `country_markup` 
                       WHERE estatus = '1' 
                       AND country_id REGEXP '[[:<:]]138[[:>:]]'

以上查询返回值按百分比增加,但如果条件未满,则返回 NULL。我想要的是如果查询返回 NULL 值而不是它应该返回 1944.0000 并且为此我尝试了下面的代码但没有成功

SELECT  IF( (SELECT (CASE 
                        WHEN 1944.0000 >= country_from_price 
                            AND 1944.0000 <= country_to_price
                        THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
                            ELSE 1944.0000
                        END 
                        )   as f1
                       FROM `country_markup` 
                       WHERE estatus = '1' 
                       AND country_id REGEXP '[[:<:]]223[[:>:]]') IS NOT NULL, f1  ,1944.0000) as final_price

提前致谢

4

2 回答 2

0

您只需要测试country_percentage表达式中的 is 不为空:

SELECT    (CASE WHEN 1944.0000 between country_from_price AND country_to_price and
                     country_percentage is not null
                THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
                ELSE 1944.0000
            END)   
FROM `country_markup` 
WHERE estatus = '1' AND country_id REGEXP '[[:<:]]138[[:>:]]';

我还将比较逻辑更改为使用between.

于 2013-05-30T14:05:48.610 回答
0

这似乎适用COALESCE(假设您的原始查询只能返回一条记录):

SELECT COALESCE((
  SELECT    
      CASE WHEN 1944.0000 >= country_from_price AND 1944.0000 <= country_to_price
          THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
               ELSE 1944.0000
          END         
  FROM `country_markup` 
  WHERE estatus = '1' 
      AND country_id REGEXP '[[:<:]]138[[:>:]]'), 1944.0000) final_price

您可以添加LIMIT 1到您的子查询中,以确保它在需要时也只返回一条记录。


MAX这是使用聚合的替代方法COALESCE

  SELECT    
      COALESCE(MAX( CASE WHEN 1944.0000 >= country_from_price AND 1944.0000 <= country_to_price
          THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
               ELSE 1944.0000
          END ),1944.0000)    final_price   
  FROM `country_markup` 
  WHERE estatus = '1' 
      AND country_id REGEXP '[[:<:]]138[[:>:]]' 
于 2013-05-30T13:57:20.673 回答