根据货币订购价格

时间:2013-07-30 20:04:56

标签: mysql

我需要订购价格,但它有货币,所以它看起来并不那么容易。表:

lowmoney | topmoney | currency

1 | 99 | eur
2 | 59 | usd

以上示例意味着价格为1.99EUR和2.59 $。有大约5000个条目,我需要按实际价格订购,例如1.99EUR大于2.19 $,所以

ORDER BY lowmoney DESC, topmoney DESC

不起作用。

P.S。只有两种货币 - usd和eur。

2 个答案:

答案 0 :(得分:2)

select
  lowmoney,
  topmoney,
  currency
from TheTable
order by
  /* Order by amount in dollars (could calculate to euro as well) */
  (lowmoney + (topmoney / 100)) *
  case currency 
    when 'eur' then 1.3266 /* Current exchange rate, according to Google */
    when 'usd' then 1
  else
    null /* What to do with other currencies. */
  end desc

答案 1 :(得分:1)

说你有:1 US dollar值得0.7618 Euro

然后

使用此

select concat(`lowmoney`,',', `topmoney`)as money,currency,
       case when `currency` = 'eur' then concat(lowmoney,',' ,topmoney) 
            when `currency` = 'usd' then concat(lowmoney,',' ,topmoney) *  0.7618
            end as money_in_euro  
 from table2
 order by money_in_euro asc

DEMO HERE

输出:

   MONEY    CURRENCY    MONEY_IN_EURO
   1,99       eur            1,99
   2,59       usd            1.5236
  
      
  • 如果您有其他货币,只需添加WHEN currency ='gdgd'然后concat(...)* the_rate
  •