CREATE TABLE `information` (
  `infoId` int(11) NOT NULL auto_increment COMMENT '信息编号',
  `score1` int(11) NOT NULL default '0' COMMENT '默认加分值',
  `score2` int(11) NOT NULL default '0',
  `totalScore`,
  PRIMARY KEY  (`infoId`)
)我想这个totalScore的字段等于score1+score2  自动计算生成,
我记得sql server里 好像totalScore as  score1+score2 就可以了 可是mysql里 不行 该怎么设计呢?

解决方案 »

  1.   

    MySQL 中没有计算列这个概念。无法直接在表中实现,只能在视图中自己实现了。CREATE TABLE `information` (
     `infoId` int(11) NOT NULL auto_increment COMMENT '信息编号',
     `score1` int(11) NOT NULL default '0' COMMENT '默认加分值',
     `score2` int(11) NOT NULL default '0',
     PRIMARY KEY  (`infoId`)
    );create view v_information as 
    select infoId,score1,score2,score1+score2 as totalScore from information;
      

  2.   

    MYSQL没有计算列,直接用SQL语句解决
      

  3.   

    用视图啊
    CREATE OR REPLACE VIEW v_information AS
    SELECT infoId, score1, score2, score1 + score2 AS totalScore
    FROM information