Mysql - 如何创建自动对行求和的列?
我有一个名为“角色”的 mysql 数据库表,其中包含姓名、力量、智力、技能等列。我想创建一个自动总结力量、智力和技能的专栏。这可能吗?
我见过很多显示如何进行查询的页面。我做了一个查询,效果很好,例如 选择 (str+intel+skl) 作为字符的总和; 它返回一个总和就好了。
但我缺少(不明白)的是如何:
- 要么自动查询我的mysql数据库(例如,当我执行“select * from character”时,它将显示“力量,智力,技能,总和” ),
- 或者在哪里/如何将 mysql 查询合并到我的 Rails 应用程序中,以便 SUM 实时显示,并且当发生编辑(例如强度)时,SUM 会相应更新。
一个关键点是,我是对一行中的列(力量、智力、技能)进行求和,而不是对一列(多个角色的强度)求和。
I have a mysql database table called "character" with columns like name, strength, intelligence, skill. I want to create a column that sums up strength, intelligence and skill AUTOMATICALLY. Is this possible?
I have seen loads of pages showing how to do queries. I did a query just fine, such as
select (str+intel+skl) as sum from character;
It returns a sum just fine.
But what I'm missing (don't understand) is how to:
- either AUTOMATE that query into my mysql db (for example, when I do "select * from character", it will show "strength, intelligence, skill, sum"),
- OR where/how to incorporate the mysql query into my rails app so that SUM shows up in real time, and when edits to, for e.g. strength occur, the SUM is updated accordingly.
A key point, I am summing columns across a row (strength, intelligence, skill), not summing a column (strengths of several characters).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您最好的选择是使用触发器(或应用程序级别的类似代码)。
使用视图也可以,但如果您经常需要它,您会希望出于性能原因对其进行预先计算。
有关触发器的介绍,请参见:
http://dev.mysql.com /doc/refman/5.6/en/triggers.html
您可能想要这样的东西:
或者,在您的应用程序中预先计算总和并相应地存储它。
旁注:问问自己是否真的需要存储它。正如另一位评论者指出的那样,可能根本不需要自动计算的数据。
Your best option is to use a trigger (or the similar code at the app level).
Using a view works too, but if you need it frequently you'll want it pre-calculated for performance reasons.
For an introduction to triggers, see:
http://dev.mysql.com/doc/refman/5.6/en/triggers.html
You probably want something like this:
Alternatively, pre-calculate the sum in your app and store it accordingly.
Side note: ask yourself if you really need this stored though. As point out by another commenter, there might be no need for the auto-calculated data at all.
如果我正确理解你的问题并且力量、智力、技能列是数字数据类型,这应该可行。
根据建议,可以使用以下命令轻松创建视图:
This should work, if I'm understanding your question correctly and the strength, intelligence, skill columns are numeric data types.
As suggested, a view could then be created pretty easily with:
您可能希望将其放入存储过程中。该过程可以进行计算,并且您可以像列一样读取结果。
对于这个用例,您也可以使用简单的视图。
You would want to make this into a stored procedure. The procedure could do the calculations, and you could read the results as though they were columns.
You could also probably get away with a simple view for this use case.
不要在 mysql 中这样做,只是在显示它们时将值相加,说真的。
或者,如果您确实想要,请在返回它们时在 SELECT 语句中将它们汇总到 mysql 中。
不要仅仅为了维护一些数字的总数而使用触发器——它们是一种非常扭曲的方式,会让项目的所有未来开发人员感到困惑。
Don't do this in mysql, just add the values up when displaying them, seriously.
Or if you really want, sum them in mysql in your SELECT statement when returning them.
Don't use a trigger just to maintain a total of some numbers - they are a seriously screwy way of confusing all future develoeprs on the project.