我可以在 MySQL 数据库的列中输入公式吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3541277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I enter formula in the column for MySQL database?
提问by King
I wonder if the above can operate the column like the excel.
不知道上面的能不能像excel一样操作列。
eg. same row. column 1 : A, column 2 : b, column 3 : A + b.
例如。同一行。第 1 栏:A,第 2 栏:b,第 3 栏:A + b。
采纳答案by Martin Smith
It doesn't look like MySQL supports computed columns as per SQL Server.
看起来 MySQL 不像 SQL Server 那样支持计算列。
You could use a Viewwith these calculated columns in or (if you want the value of the calculation to be persisted so you can search against it using an index) add a column and keep it up-to-date with triggers
您可以使用带有这些计算列的视图或(如果您希望计算的值被持久化,以便您可以使用索引对其进行搜索)添加一列并使用触发器使其保持最新
回答by Pekka
You can't have columns that automaticallycontain some neighbouring cell's value or do some calculations with it.
您不能让列自动包含某些相邻单元格的值或使用它进行一些计算。
However, in a mySQL query, you can do all the things Excel can, and much much more.
但是,在 mySQL 查询中,您可以执行 Excel 可以执行的所有操作以及更多操作。
For example, to get the sum of two int
fields:
例如,要获取两个int
字段的总和:
SELECT column_a, column_b, (column_a + column_b) as total FROM tablename
However, looking at your other questions, I'm not sure whether mySQL is really what you are looking for. It sounds to me like you need an application just like Excel.
但是,查看您的其他问题,我不确定 mySQL 是否真的是您要查找的内容。在我看来,您需要像 Excel 这样的应用程序。
回答by Haim Evgi
you can create a view of the table like you mention
你可以像你提到的那样创建表格的视图
like :
喜欢 :
create view myview as select a,b,(a+b) as c from table
回答by Thomas Clayson
mysql is a database and not a spreadsheet so no you can't, and you probably shouldn't be anyway.
mysql 是一个数据库而不是电子表格,所以不,你不能,而且你可能不应该这样做。
I suppose the point is that a spread sheet holds AND displays the data - mysql holds the data then you use php to show the data (or similar).
我想重点是电子表格保存并显示数据 - mysql 保存数据,然后您使用 php 显示数据(或类似数据)。
When you retrieve from the database you can do:
从数据库中检索时,您可以执行以下操作:
SELECT (A+B) AS c FROM table
or when you put into the database you can do the maths.
或者当您放入数据库时,您可以进行数学运算。
回答by dsimer
I come from an Excel background myself and found that to be one of the challenges of moving over to databases, but I realize now that data is rarely presented as-is from a table. Chances are you will be using a view because you or your end users will want to see it laid out a certain way.
我自己有 Excel 背景,并发现这是迁移到数据库的挑战之一,但我现在意识到数据很少从表格中按原样呈现。您可能会使用视图,因为您或您的最终用户希望以某种方式查看它。
Pretty much any time you think you want a calculated field, chances are you would do well to create a view that performs the calculation. it also keeps the load off of any application you might be developing to use the info, because the calculations are done server-side.
几乎任何时候你认为你想要一个计算字段,你很可能会创建一个执行计算的视图。它还可以减轻您可能正在开发以使用该信息的任何应用程序的负载,因为计算是在服务器端完成的。
回答by Christopher Poole
- If you need the function of Excel but need it to store larger data sets using MySQL is fine. However, to get the calculated columns you need to use an interface and Microsoft makes one called Access. You can use Access to interact with your database and build queries to return your answers the way you want them.
- 如果您需要 Excel 的功能,但需要它使用 MySQL 存储更大的数据集,那很好。但是,要获得计算出的列,您需要使用一个界面,Microsoft 制作了一个名为 Access 的界面。您可以使用 Access 与您的数据库进行交互并构建查询以按照您希望的方式返回您的答案。
This would keep you from having to build a web interface to your MySQL database to accomplish the same thing.
这将使您不必为您的 MySQL 数据库构建一个 Web 界面来完成同样的事情。