【发布时间】:2021-02-23 03:07:35
【问题描述】:
我是 SQL 新手,我正在尝试在我的 SQL 查询中添加条件。
我每年有 4 个产品定价文件和另一个产品表。
price_table_2020
price_table_2019
price_table_2019
所有表的数据如下
price_table_2020:
product price product_description
-------------------------------------
ball 100 plastic ball
ink 80 ink for pen
pen 1000 pen
bucket 200 bucket
price_table_2019:
product price product_description
------------------------------------
ball 90 plastic ball
ink 70 ink for pen
pen 900 pen
bucket 100 bucket
price_table_2018:
product price product_description
-------------------------------------
ball 80 plastic ball
ink 60 ink for pen
pen 800 pen
bucket 300 bucket
产品表如下所示:
product invoice_year
---------------------
pen 2019
ball 2020
ink 2020
根据发票年份,我应该从相应的表格中获取相应产品的价格。
预期输出如下:
product invoice_year price
----------------------------
pen 2019 900
ball 2020 100
ink 2020 80
我可以从下面的一张表中得到价格
select A.price, B.product, B.product_description
from product A
inner join price_table_2020 B on A.product = B.product
如果我必须包含基于product.invoice_year 的逻辑,有人可以帮忙吗?谢谢。
【问题讨论】:
-
您的 DBMS 是什么?您应该始终使用您要求的 DBMS 标记 SQL 请求。这些产品的 SQL 方言可能完全不同。
-
注明@ThorstenKettner
标签: sql select conditional-statements