从一个 table 获取值到另一个

getting values from one table to other

我正在使用一个名为交易和销售的 tables,其中交易包含帐号、脚本名称、交易类型 (transactio) 和数量; sales table 包含脚本名称、帐号、shares_bought 和 shares_sold。 如果交易类型是卖出,则 shares_bought = 数量,否则 Shares_sold= 数量。我写的SQL命令不起作用,请指正。

SELECT t.scriptname, s.accnum, s.shares_bought, s.shares_sold
FROM sales AS s INNER JOIN transac AS t ON s.accnum = t.accnum
SELECT CASE WHEN t.transactio = 'buy' THEN s.shares_bought=t.Quantity 
ELSE s.shares_sold=t.Quantity 
END

例如我在交易中有 table 我有 scriptname="abcd" accnum="1" transactio="buy" 和 Quantity="20" 在 sales table 中,我应该得到 scriptname="abcd" accnum="1" shares_bought="20" 并且股票卖空

如果我理解正确,你应该将第二个 select 替换为 and 或 where

SELECT t.scriptname, s.accnum, s.shares_bought, s.shares_sold
FROM sales s INNER JOIN transac t ON s.accnum = t.accnum
and CASE WHEN t.transactio = 'sell' THEN s.shares_sold=t.Quantity 
ELSE s.shares_sold=t.Quantity 
END