2
SELECT user_id, currency, SUM( price ) AS totalPrice, created
FROM accounts WHERE user_id IN ( SELECT users.id FROM users
                                 WHERE country_id ='$con_id' )
AND created BETWEEN  '2013-02-01' AND  '2013-02-28' GROUP BY currency

这个查询给了我类似的结果

Curreny  totalPrice
SEK      10
USD      10

我只想从 7 乘以瑞典克朗。我在数据库中只有美元汇率,所以我想在查询中将它乘以7以获得瑞典克朗汇率。我怎样才能在这个查询中做到这一点?

4

3 回答 3

2

你的问题有点令人困惑。如果我正确理解您的问题,您想将总价值乘以7货币是否等于SEK

SELECT  user_id, 
        currency, 
        SUM(price) * IF(currency = 'SEK', 7, 1) totalPrice, 
        created
FROM    accounts
WHERE   user_id
        IN 
        (
            SELECT users.id
            FROM users
            WHERE country_id =  '$con_id'
        ) AND 
        created BETWEEN  '2013-02-01' AND  '2013-02-28'
GROUP   BY currency
于 2013-03-17T09:52:17.283 回答
0

我会将所有转换率存储在一个表中。对于 SEK,您存储 7.0 als 转换率和 vor USD 1.0。只需将总价格乘以转化率(带子查询)

于 2013-03-17T09:51:38.737 回答
0

这是一个丑陋的实现方式:

SELECT user_id, currency,
       CASE WHEN currency = 'SEK' THEN SUM(price)*7
       ELSE SUM(price) END totalPrice, created
FROM accounts
WHERE user_id
IN ( SELECT users.id FROM users
     WHERE country_id =  '$con_id')
AND created BETWEEN  '2013-02-01' AND  '2013-02-28'
GROUP BY currency
于 2013-03-17T09:53:32.733 回答