在mysql中创建表,其中一列包含另外两列的值之和 [英] Create table in mysql with one column containing sum of another two columns value

查看:644
本文介绍了在mysql中创建表,其中一列包含另外两列的值之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mysql中是否可以创建一个表,该表的列将两个列的值组合在一起?像这样的东西:

Is it possible in mysql to create a table with a column that combines two column values? something like this:

create table test1 (
    number1 int,
    number2 int,
    total int DEFAULT (number1+number2)
);

或这样:

CREATE TABLE `Result` (
    `aCount` INT DEFAULT 0,
    `bCount` INT DEFAULT 0,
    `cCount` =  `aCount` + `bCount`
);

推荐答案

不可能完全做到这一点,但是您可以基于将它们结合在一起的查询来创建视图:

It is not possible to do that exactly, but you can create a view based on a query that combines them:

CREATE VIEW `my_wacky_view` AS
SELECT `number1`, `number2`, `number1` + `number2` AS `total`
FROM `test1`;

除非您要运行大量查询,这些查询将在其WHERE子句中引用组合数据,否则我将避免将组合数据实际存储在表中.

I would avoid actually storing the combined data in a table, unless you're going to be running lots of queries that will reference the combined data in their WHERE clauses.

这篇关于在mysql中创建表,其中一列包含另外两列的值之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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