我想知道是否有人可以帮助解决数据库问题。我正在使用 Rails 5 和 MySQL;
我想创建一个有 3 个整数列的表。第 1 列和第 2 列由用户填充,第 3 列是 c1xc2 的结果,应存储为整数。我将如何创建这样一个表?我可以很高兴地创建没有最后计算列的表,只是不确定最好的方法。
我确实考虑过,也许我根本不需要最后一列,而是每次需要计算时,我可以使用 erb 引用 c1 和 c2 来呈现结果
将产品存储在表中有什么好处(如果有的话)/
非常感谢。
最佳答案
What are the benefits if any to storing the product in the table
如果您没有关于该主题的任何具体信息(“好处”) - 那就没什么了。
I did consider that maybe I don't need the final column at all, but rather each time the calculation was needed I could render the results using erb referring to c1 and c2
确实,你不知道。在
SELECT col1, col2, (col1 + col2) AS col3 FROM yourtable WHERE col1 = <something>;
与记录查找/获取相比,总和不需要任何成本(与建议的触发解决方案不同)。
关于mysql - Rails 5 MySQL : Table column as sum of two other columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378763/