SQL 将 2 个不同表中的 2 个值相乘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5990586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:31:57  来源:igfitidea点击:

Multiply 2 values from 2 different tables

sql

提问by user752160

I'm trying to multiply value X by value Y with SQL. Value X is located in table A and B is located in table B. I couldn't find the answer for this.

我正在尝试使用 SQL 将值 X 乘以值 Y。值 X 位于表 A 中,B 位于表 B 中。我找不到答案。

Table Transactions

表事务

ID Transaction_ID Total_Amount
1  001            1200
2  002            1500
3  003            1600

Table Rates

表费率

ID Currency_Name Exchange_Rate
1  AUD           1.5
2  SEK           2.0
3  PLN           3.0

The question I'm trying to answer is:

我试图回答的问题是:

What is the Total_Amount for transaction 001 in SEK (Swedish Crown). So I need to multiply 1200 * 2.0 and display the result.

SEK(瑞典克朗)中交易 001 的 Total_Amount 是多少。所以我需要乘以 1200 * 2.0 并显示结果。

回答by TBohnen.jnr

Edited based on added info

根据添加的信息进行编辑

SELECT Total_Amount * Exchange_Rate AS Value 
FROM Transactions, Rates 
WHERE Rates.Currency_Name = 'Sek' and Transaction_id = 001

回答by garnertb

To answer your question:

回答你的问题:

What is the Total_Amount for transaction 001 in SEK (Swedish Crown). So I need to multiply 1200 * 2.0 and display the result.

SEK(瑞典克朗)中交易 001 的 Total_Amount 是多少。所以我需要乘以 1200 * 2.0 并显示结果。

Use:

用:

SELECT ID, Transaction_ID, Total_Amount, Total_Amount*(SELECT Exchange_Rate from Rates where Currency_Name='SEK') 
FROM TRANSACTIONS
WHERE TRANSACTION_ID='001'

回答by Harry Joy

how about this:

这个怎么样:

select a.x*b.y from tableA a, tableB b

回答by krtek

This should work, depending on your table structure :

这应该有效,具体取决于您的表结构:

select table1.x * table2.y
from table1, table2;

But I actually doubt that this is really what you want to do, if you provide more information, we could give you a much better answer. Please provide table structure and you're real goal !

但我实际上怀疑这是否真的是您想要做的,如果您提供更多信息,我们可以为您提供更好的答案。请提供表格结构,您才是真正的目标!