根据货币的订单价格

问题描述:

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

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。

+0

的SQL数据库没有之间的转换率的概念二,你将需要返回所有结果并在查询之外执行此操作。 –

+0

这是不可能的,SQL数据库将需要一些不断变化的转换率来源。所以你必须自己做这个。 –

+0

这将以货币变化率计算 –

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 
+0

谢谢你,作品像一个魅力 –

说你有: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货币= 'gdgd' 然后CONCAT(...)* the_rate