create database test
gouse test
create table t
(
a int primary key,
b int not null,
c int not null,
d as b/c
)
上面是个例子不考虑c为0,本意是弄计算列让d为b除c的结果,而上面的b/c是取模,该怎么做?d并没有声明类型,数据库是根据b和c来判断d的类型的吧。测试b c 为float型时可以。怎么做可以达到d 为 b除c的结果呢(b c为整数),
gouse test
create table t
(
a int primary key,
b int not null,
c int not null,
d as b/c
)
上面是个例子不考虑c为0,本意是弄计算列让d为b除c的结果,而上面的b/c是取模,该怎么做?d并没有声明类型,数据库是根据b和c来判断d的类型的吧。测试b c 为float型时可以。怎么做可以达到d 为 b除c的结果呢(b c为整数),
create table t
(
a int primary key,
b int not null,
c int not null,
d as CAST(b AS DECIMAL(18,2))/c
)
INSERT INTO t(a,b,c)
SELECT 1,2,3 UNION ALL
SELECT 3,6,7 UNION ALL
SELECT 6,7,8SELECT * FROM t
/*
a b c d
----------- ----------- ----------- ---------------------------------------
1 2 3 0.6666666666666
3 6 7 0.8571428571428
6 7 8 0.8750000000000
*/