如何在查询中只乘以一行?

时间:2013-03-17 09:42:08

标签: mysql

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增加 SEK 。我在数据库中只有 USD 率,所以我想在查询中将其乘以 7 < / strong>获得 SEK 率。我怎么能在这个查询中做到这一点?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我会将所有转化率存储在一个表格中。对于SEK,您可以存储7.0 als转换率和1.0美元。只需将totalPrice与转换率(带子查询)相乘

答案 2 :(得分: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