问题
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 codequantity
: the quantity of an itemprice
: 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