Python 从2个表中创建一个表作为结果
我正在尝试创建一个表,作为两个表Prices and Sales的结果 该表应包括4列产品/日期/金额/收入 其中前3列为销售表的总副本,收入列应为=Sales.amount*prices.price 结果我得到了一张空桌子Python 从2个表中创建一个表作为结果,python,mysql,sql,Python,Mysql,Sql,我正在尝试创建一个表,作为两个表Prices and Sales的结果 该表应包括4列产品/日期/金额/收入 其中前3列为销售表的总副本,收入列应为=Sales.amount*prices.price 结果我得到了一张空桌子 cursor.execute("CREATE TABLE revenue(product VARCHAR(1), date DATE, amount FLOAT, revenue FLOAT);") cursor.execute("INSERT into revenue S
cursor.execute("CREATE TABLE revenue(product VARCHAR(1), date DATE, amount FLOAT, revenue FLOAT);")
cursor.execute("INSERT into revenue SELECT sales.product, sales.date, sales.amount, prices.price*sales.amount FROM sales, prices")
conn.commit()
首先,避免使用
隐式联接
,因为它们已经被弃用了一段时间,如果需要,可以使用[date]等保留字。如果列名有空格/特殊字符或包含SQL关键字,则必须将列名括起来:
INSERT INTO revenue (product , [date], amount , revenue)
SELECT s.product, s.[date], s.amount,p.price*s.amount
FROM sales AS s
INNER JOIN prices AS p ON (join condition)
如果遇到任何转换问题,您可能需要
CAST()
作为prices.price*sales.amount
的一部分。我相信您可以这样做:
CREATE TABLE revenue as
select s.product, s.`date`, s.amount, p.price * s.amount
from sales s
join prices p on s.product = p.product;
最后一行:
join prices p on s.product = p.product;
是我假设的连接条件可能是
你好@VladimirDemo,欢迎来到stack Owerflow。你能给我们看两行来自销售表的数据和两行来自价格表的数据吗?并向我们展示预期的结果。