选择语句MySQL中的CASE

时间:2016-04-18 05:47:00

标签: mysql database

我在mysql查询中有错误,所以我需要一个基于前一个选择值的if语句。

这是我的查询,

[myInputView becomeFirstResponder];

我的错误发生在case语句中,我需要case语句来读取conv_taxrate_commission中的值。如果mcr.curr_type ='LOCAL'我需要conv_taxrate_commission等于dsp.curr_amount,否则我需要按原样保留conv_taxrate_commission。

请帮帮我

1 个答案:

答案 0 :(得分:1)

试试这个,我假设当mcr.curr_type ='LOCAL'时,final_conv_ratedsp.curr_amount;)

SELECT
  ds.master_rcpt_gen,
  ds.visa_rcpt_gen,
  dsp.curr_amount,
  dsp.curr_id,
  mcr.conv_rate,
  mcr.curr_code,
  mcr.curr_desc,
  mcr.curr_type,
  (conv_rate / (SELECT mcr.conv_rate
                FROM mst_currencies_rate mcr
                WHERE mcr.curr_code = 'USD'))                                                 conv_ratio,
  curr_amount * (conv_rate / (SELECT mcr.conv_rate
                              FROM mst_currencies_rate mcr
                              WHERE mcr.curr_code = 'USD'))                                   conv_usd,
  CASE WHEN mcr.curr_type = 'LOCAL'
    THEN dsp.curr_amount
  ELSE
    (SELECT mtr.USD_tax_rate
     FROM mst_tax_rate mtr) * (curr_amount * (conv_rate / (SELECT mcr.conv_rate
                                                           FROM mst_currencies_rate mcr
                                                           WHERE mcr.curr_code = 'USD'))) END conv_taxrate_commission,
  CASE WHEN mcr.curr_type = 'LOCAL'
    THEN dsp.curr_amount
  ELSE (SELECT mtr.USD_tax_rate
        FROM mst_tax_rate mtr) * (curr_amount * (conv_rate / (SELECT mcr.conv_rate
                                                              FROM mst_currencies_rate mcr
                                                              WHERE mcr.curr_code = 'USD')))
  END                                                                                         final_conv_rate
FROM
  dtl_sr ds
  INNER JOIN dtl_sr_payment dsp ON dsp.flight_id = ds.flight_id
                                   AND dsp.sr_id = ds.SR_ID
  INNER JOIN mst_currencies_rate mcr ON mcr.curr_id = dsp.curr_id