mysql - Rails 5 MySQL : Table column as sum of two other columns

标签 mysql datatables ruby-on-rails-5

我想知道是否有人可以帮助解决数据库问题。我正在使用 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/

相关文章:

mysql - Powershell 哈希表和重复键

python - Sam 本地的 mysql 数据库连接

mysql - 数据库关系模式 MySQL - 配置文件

mysql 在列表中选择正则表达式/子字符串

html - DataTables 中的自动换行

javascript - 为什么固定列不能使用数据表工作?

jquery - 使用 DataTables 导出到 PDF 时格式化特定的列内容样式

ruby-on-rails - 获取所有没有 child 的记录,即使它们存在于Elasticsearch中

java - 如何将系统属性从jruby传递到Rails服务器

ruby - 测试环境中 Rails.application.secrets 中缺少 secret_key_base