简体   繁体   English

如何将结果乘以另一个表中的特定值?

[英]How can I multiply a result by a specific value from another table?

A simplification of my problem. 我的问题的简化。 Say I have these tables: 说我有这些表:

Supply: 供应:

  • supplyName: VARCHAR(20), Just the name of an office supply. supplyName:VARCHAR(20),仅是办公用品的名称。 Say pens. 说笔。
  • quantityPerBox: INT, the number of the supply that comes in a box. quantityPerBox:INT,一个盒子中的供应品编号。

Boxes: 盒:

  • supplyName: VARCHAR(20), Name of a supply, references the Supply table. supplyName:VARCHAR(20),供应名称,引用了供应表。
  • numBoxes: INT, the number of boxes of the supply. numBoxes:INT,货源的箱数。

Basically want I want to do is: 基本上我想做的是:

SELECT supplyName, numberOfSupply GROUP BY supplyName

from these tables. 从这些表。 But I want numberOfSupply to be quantityPerBox*numBoxes. 但我希望numberOfSupply是quantityPerBox * numBoxes。

Can this be done as a query? 可以作为查询吗? Or would I need to store the quantities from the Supply table in a program in some language (PHP for instance) and do the calculations there? 还是我需要以某种语言(例如PHP)将“供应”表中的数量存储在程序中,并在那里进行计算? (Or is there really no benefit from doing the calculation as part of the query, even if it's possible?) (或者即使有可能,将计算作为查询的一部分真的没有好处吗?)

Thank you for your help in advance. 提前谢谢你的帮助。 It's appreciated. 感激不尽

This can certainly be done in a query without having to store that value separately, or using a different language: 当然,这可以在查询中完成,而不必分别存储该值或使用其他语言:

SELECT s.supplyName, s.quantityPerBox * b.numBoxes AS NumberOfSupply
FROM Supply AS s
INNER JOIN Boxes AS b ON s.supplyName = b.supplyName

You probably want to add SupplyId to both tables though so you have an integer value to JOIN on: more efficient than trying to JOIN on a VARCHAR field. 不过,您可能想将SupplyId添加到两个表中,以便在JOIN上具有一个整数值:比在VARCHAR字段上尝试JOIN效率更高。 Another benefit is that if you ever wanted to change the supplyName of an item, you would not have to worry about foreign key issues when making that change if you were referencing tables by ID instead of a value within the table. 另一个好处是,如果您想更改supplyName ,则在更改时,如果您是通过ID而不是表中的值来引用表,则不必担心外键问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:如何根据另一个表的结果选择一个值(日期)并将其包含在所述结果中? - MySQL: How can I select a value (date) based on a result from another table and include it in said result? 从表单输入中获取值,将其乘以MySQL表单元格值,然后将结果写入同一表中的另一个单元格 - Take value from form input, multiply it by MySQL table cell value and write result to another cell in same table 如何基于另一个表中的列将列中的所有值相乘? - How can I multiply all values in a column based on a column from another table? 如何使用 LastInsertId 插入另一个表中的值 - How can I use LastInsertId to insert a value from another table 我可以插入来自另一个方案的查询表结果吗? - Can I insert into table result of query from another scheme? 如何乘以mysql的结果? - How to multiply result from mysql? 当我需要从另一个表中了解外键的值时,如何将数据插入表中? - How can I insert data into a table when I need to know the value of the foreign key from another table? Thymeleaf如何根据另一个表中的值显示一个表中的列 - Thymeleaf How can I display a column from one table based on a value from another table 如何通过乘以结果查询特定的行顺序 - How to query specific rows order by multiply result SQL 如何找到不在另一个结果中的特定值 - SQL How to find specific value that is not in another result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM