Calculated Field Using Data From Another Table

风格不统一 提交于 2020-02-08 05:37:05

问题


I have a table set up that needs to have a field that calculates the price of an order. To do so, I have a 3-letter code set up for each item, but the price of these items isn't included in that table, but another table. To calculate the price, I need to multiply the quantity of the item by the price of the item. So basically, how do I associate the 3-letter code with the price of the item in the other table. The 3 letter code field is also included in the table with the price of the item


回答1:


What you need is a join:

   SELECT table1.quantity * table2.price AS result
     FROM table1
LEFT JOIN table2
       ON table1.code = table2.code

Assuming table1 and table2 have the following fields (you might want to adjust as necessary):

  • code: the 3-letter code
  • quantity: the quantity of an item
  • price: the price of the item

Also see here for more background information: SQL JOIN and different types of JOINs



来源:https://stackoverflow.com/questions/37507531/calculated-field-using-data-from-another-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!