当数据库以不同货币存储价格时,产品的最低价格选择

时间:2014-01-26 13:13:44

标签: mysql currency minimum

我在美国(美国),英国(英国)等不同的在线商店中收集产品的价格,并以当地货币将其保存在一个mysql TABLE(com)中。 例如,对于美国的美元价格,对于英国 - 以英镑为单位。

TABLE com

 ---------------------------------------------------------------------------------------
| AUTO_INC | COUNTER |   ID |      CONC | VOLUME | PRICE |      SHOP | DATE_G | COUNTRY |
|----------|---------|------|-----------|--------|-------|-----------|--------|---------|
|   115124 |   76720 | 2399 | prod_name |     13 | 34.23 | store1.us |      3 |      us |
|   115186 |   50952 | 2399 | prod_name |     13 |    36 | store2.us |      3 |      us |
|   115187 |   45828 | 2399 | prod_name |     13 | 37.44 | store3.us |      3 |      us |
|   116448 |   73419 | 2399 | prod_name |   11.6 |    48 | store4.us |      3 |      us |
|   116449 |   73421 | 2399 | prod_name |     13 |  65.5 | store4.us |      3 |      us |
|   133334 |   22154 | 2399 | prod_name |     13 | 36.95 | store5.us |      4 |      us |
|   133386 |   31646 | 2399 | prod_name |     13 | 37.44 | store3.us |      4 |      us |
|   134828 |   54667 | 2399 | prod_name |   11.6 |    48 | store4.us |      4 |      us |
|   134929 |   54670 | 2399 | prod_name |     13 |  65.5 | store4.us |      4 |      us |
|   133337 |   22155 | 2399 | prod_name |     13 | 26.95 | store1.uk |      4 |      uk |
|   133387 |   31647 | 2399 | prod_name |     13 | 17.44 | store2.uk |      4 |      uk |
|   134829 |   54668 | 2399 | prod_name |   11.6 |    30 | store3.uk |      4 |      uk |
|   134830 |   54671 | 2399 | prod_name |     13 |  45.5 | store4.uk |      4 |      uk |
 ---------------------------------------------------------------------------------------

要将价格从本地货币转换为美元,我已创建了TABLE my_currency

表my_currency

 --------------------------------------
| AUTO_INC | DOMAIN | EX_RATE | DATE_G |
|----------|--------|---------|--------|
|      235 |     uk |  0.6066 |      4 |
|      236 |     us |       1 |      4 |
|      237 |     uk |  0.6066 |      3 |
|      238 |     us |       1 |      3 |
 --------------------------------------

如果我想为我们的网上商店选择最低价格(其中COUNTRY ='us'),我使用以下查询:

SELECT t1.* FROM com as t1
INNER JOIN (
  SELECT id, conc, volume, min(price) as usd_price, date_g 
  FROM com 
  WHERE id=2399 AND date_g=4 AND country='us'
  GROUP BY conc, volume) as t2 
ON t1.conc=t2.conc and t1.volume=t2.volume and t1.id=t2.id and t1.price=t2.usd_price and t1.date_g=t2.date_g
ORDER BY conc DESC, volume DESC

我得到了正确的结果:

| AUTO_INC | COUNTER |   ID |      CONC | VOLUME | PRICE |      SHOP | DATE_G | COUNTRY |
|----------|---------|------|-----------|--------|-------|-----------|--------|---------|
|   133334 |   22154 | 2399 | prod_name |     13 | 36.95 | store5.us |      4 |      us |
|   134828 |   54667 | 2399 | prod_name |   11.6 |    48 | store4.us |      4 |      us |

但现在我的目标是从所有商店(('us','uk')中选择最低价格)并考虑到该数据库以当地货币持有价格: 1.将当地货币的价格转换为美元 2.选择美元的最低价格

所以我尝试使用以下查询:

SELECT t1.auto_inc, t1.id, t1.conc, t1.volume, (t1.price / my_currency.ex_rate) as sub_price, t1.date_g 
FROM com as t1
 inner join my_currency 
 ON t1.country=my_currency.domain AND t1.date_g=my_currency.date_g
inner join (
 select com.id, com.conc, com.volume, min(com.price / my_currency.ex_rate) as usd_price, com.date_g 
 from com 
 inner join my_currency 
 ON com.country=my_currency.domain AND com.date_g=my_currency.date_g
 WHERE com.id=2399 AND com.date_g=4 AND com.country in ('us', 'uk')
 GROUP BY conc, volume) as t2 
on 
t1.id=t2.id and 
t1.conc=t2.conc and 
t1.volume=t2.volume and 
(t1.price / my_currency.ex_rate)=t2.usd_price and
t1.date_g=t2.date_g 
ORDER BY conc DESC, volume DESC

但是我收到了INCORRECT结果:

| AUTO_INC |   ID |      CONC | VOLUME | SUB_PRICE | DATE_G |
|----------|------|-----------|--------|-----------|--------|
|   134828 | 2399 | prod_name |   11.6 |        48 |      4 |

CORRECT结果是:

| AUTO_INC |   ID |      CONC | VOLUME | SUB_PRICE | DATE_G |
|----------|------|-----------|--------|-----------|--------|
|   134828 | 2399 | prod_name |   11.6 |        48 |      4 |
|----------|------|-----------|--------|-----------|--------|
|   133387 | 2399 | prod_name |   13   | 28.750412 |      4 |

有人有什么想法吗? sqlfiddle.com

1 个答案:

答案 0 :(得分:1)

问题是计算的十进制值上的join。以下作品:

SELECT t1.auto_inc, t1.id, t1.conc, t1.volume,
       (t1.price / my_currency.ex_rate) as sub_price, t1.date_g 
FROM com as t1 inner join
     my_currency 
     ON t1.country = my_currency.domain AND t1.date_g = my_currency.date_g
     inner join
     (select com.id, com.conc, com.volume, min(com.price / my_currency.ex_rate) as usd_price, com.date_g 
      from com inner join
           my_currency 
           ON com.country = my_currency.domain AND
              com.date_g = my_currency.date_g
      WHERE com.id=2399 AND com.date_g = 4 AND
            com.country in ('us', 'uk')
      GROUP BY com.id, com.conc, com.volume
     ) as t2 
     on t1.id = t2.id and 
        t1.conc = t2.conc and 
        t1.volume = t2.volume and 
        abs((t1.price / my_currency.ex_rate) - t2.usd_price) < 0.01 and
        t1.date_g = t2.date_g
ORDER BY conc DESC, volume DESC;

但是,如果您将join上的price条件更改为:

        t1.price / my_currency.ex_rate =  t2.usd_price and

然后它不起作用。

如果您将两个结果都投回decimal(10, 2)

        cast(t1.price / my_currency.ex_rate as decimal(10, 2)) = cast(t2.usd_price as decimal(10, 2))

它可能与文档中的note有关:

  

在使用/执行的除法中,使用两个时的结果比例   精确值操作数是第一个操作数的比例加上值   div_precision_increment系统变量(由4表示)   默认)。例如,表达式5.05 / 0.014的结果有一个   小数点后六位(360.714286)