使用另一个表中的数据计算字段 [英] Calculated Field Using Data From Another Table

查看:71
本文介绍了使用另一个表中的数据计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个表格,该表格需要一个用于计算订单价格的字段.为此,我为每个商品设置了3个字母的代码,但这些商品的价格未包含在该表中,而是包含在另一个表中.要计算价格,我需要将商品的数量乘以商品的价格.因此,基本上,我如何将3个字母的代码与另一个表中商品的价格相关联.表格中还包含3个字母的代码字段,其中包含商品的价格

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

推荐答案

您需要的是 join :

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

假设 table1 table2 具有以下字段(您可能需要根据需要进行调整):

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

  • code:3个字母的代码
  • quantity:一件物品的数量
  • price:商品价格
  • code: the 3-letter code
  • quantity: the quantity of an item
  • price: the price of the item

另请参阅此处以获取更多背景信息: SQL JOIN和不同类型的JOIN

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

这篇关于使用另一个表中的数据计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆