SQL字段作为其他字段的总和 [英] SQL field as sum of other fields

查看:74
本文介绍了SQL字段作为其他字段的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与查询无关,我想知道的是,是否有可能在一个列中显示一个字段作为其他字段的总和.有点像Excel.

This is not query related, what I would like to know is if it's possible to have a field in a column being displayed as a sum of other fields. A bit like Excel does.

作为一个例子,我有两个表:

As an example, I have two tables:

食谱

nrecepie integer
name varchar(255)
time integer

和另一个

说明

nintrucion integer
nrecepie integer
time integer

所以,基本上,因为一个食谱有n条指令,我想要

So, basically as a recipe has n instructions I would like that

recipes.time = sum(intructions.time)

这可以在创建表脚本中完成吗?如果可以,怎么办?

Is this possible to be done in create table script?? if so, how?

推荐答案

您可以使用视图:

CREATE VIEW recipes_with_time AS
SELECT nrecepie, name, SUM(Instructions.time) AS total_time
FROM Recepies
JOIN Instructions USING (nrecepie)
GROUP BY Recepies.nrecepie

如果您真的想在真实表中包含这些数据,则必须使用触发器.

If you really want to have that data in the real table, you must use a trigger.

这篇关于SQL字段作为其他字段的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆