这里是新的(加上对英语不好的抱歉..),我在使用这里的两个表时遇到了一些 SQL 困难 - 销售和产品:
Product:
ProductID | Product | Price
____________________________
1 | walnuts | 16
2 | cashew | 25
3 | peanuts | 4
Sells (each product kilograms sold):
Day | walnuts | cashew | hazelnut
__________________________________
1 | 2 | 3 | 1
2 | 8 | 6 | 25
3 | 1 | 3 | 12
Run Code Online (Sandbox Code Playgroud)
我想显示的是第 2 天赚到的总钱(例如),那么我该如何计算呢?
小智 5
你设计桌子的方式不好。
**Item table:**
product_id | product_name | price
__________________________________
1 | Some1 | 20
2 | Some2 | 30
3 | Some3 | 40
**sale table :**
sale_id | product_id | quantity
__________________________________
1 | 2 | 2
2 | 1 | 1
3 | 3 | 2
Run Code Online (Sandbox Code Playgroud)
现在应用查询:
SELECT sale.quantity*item.price as TOTAL FROM item,sale WHERE item.product_id=sale.product_id;
Run Code Online (Sandbox Code Playgroud)
如果您有更多列,则可以应用更多过滤器。