python和mysql中的矩阵乘法 [英] Matrix multiplication in python and mysql

查看:98
本文介绍了python和mysql中的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个货币兑换字典,如下所示:

I have a currency exchange dictionary, as follows:

exchange_rates = {'USD': 1.00000,
                  'EUR': 1.32875,
                  'GBP': 1.56718, ...}

然后,我使用以下方法检索产品的销售信息:

Then I retrieve the sales information for a product, using:

SELECT price, currency FROM sales

大约有一百万行,可能看起来像这样:

There are perhaps a million rows, which may look like this:

- 2.99    USD
- 3.01    EUR
- etc.

我该如何进行矩阵乘法以获取美元的总和?

How would I do matrix multiplication to get the total sum in USD?

推荐答案

不是从数据库中获取一百万行并用Python进行计算,而是将字典提供给数据库并让数据库进行计算并将其发送给您返回结果.

Instead of getting one million rows from the database and doing the calculation in Python, give your dictionary to the database and get the database to do the calculation and send you back the result.

您可以通过进行类似于以下内容的查询来做到这一点:

You can do this by making a query similar to the following:

SELECT SUM(price * exchange_rate) AS total
FROM sales
LEFT JOIN
(
    SELECT 'USD' AS currency, 1.00000 AS exchange_rate
    UNION ALL
    SELECT 'EUR', 1.32875
    UNION ALL
    SELECT 'GBP', 1.56718
    -- ...
) AS exchange
ON exchange.currency = sales.currency

这篇关于python和mysql中的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆