我现在有一张表,表里边有字段 a b c d e f g7个字段,
我现在想要的效果是字段e=a+b-c,f=a+b+c+d,g=e+f,不知道这样的SQL语句该怎么写那,如果说要用到存储过程的话,该怎么去定义,

解决方案 »

  1.   

    这样的表结构不好。
    对于e,f,g这样的因变量,同步是个大问题。一般可以采用trigger来完成同步,但是过于复杂和脆弱。
    建议表内仅建立abcd是个字段,然后使用view来得到e,f,gdrop table tbl
    create table tbl(
    a int,
    b int,
    c int, 
    d int,
    )
    create view tbl_view1 as select a,b,c,d,a + b-c as e,a+b+c+d as f ,a + b-c +a+b+c+d  as g from tbl
    insert into tbl(a,b,c,d) values(1,2,3,4)
    select * from tbl_view1或者drop table tblcreate table tbl(
    a int,
    b int,
    c int, 
    d int,
    )create view tbl_view2 as select a,b,c,d,a + b-c as e,a+b+c+d as f from tblinsert into tbl(a,b,c,d) values(1,2,3,4)select *, e+f as g from tbl_view2