我在mysql中使用trigger写了一段小程序,其中有如下一个片段:
    select     NEW.sip2dip-sip2dip  into samsip2dip from sample where port=NEW.dstport;
        if samsip2dip>0 then set samscore=samscore+samsip2dip*samsip2dip; end if;其中samsip2dip是INT类型的,但是我发现samsip2dip总是大于0,也就是说,即使NEW.sip2dip-sip2dip  本来应该是0-5=-5<0,反而给出的是一个大于0的数值,而不是-5.这个应该怎么修改呢?请指点,谢谢

解决方案 »

  1.   

    samsip2dip字段是不是定义成了INT UNSIGNED?
    对UNSIGNED数据列进行减法计算的时候返回值仍将是一个UNSIGNED整数,取值范围仅限于正数。
      

  2.   

    不是的。
    DECLARE samsip2dip  INT;
      

  3.   

    create table tcp159flow (
    rid bigint(20) unsigned NOT NULL auto_increment,
    score int(10) unsigned default '0',

    -- basic features
    srcaddr  int(10) unsigned default NULL,
    dstaddr  int(10) unsigned default NULL,
    srcport smallint(5) unsigned NOT NULL default '0',
    dstport smallint(5) unsigned NOT NULL default '0',
    numpkt  int(10)   unsigned NOT NULL default '0',
    bytes   int(10)  unsigned NOT NULL default '0',-- Start Time
    startsec   int(10)  unsigned NOT NULL default '0', 
    startmsec   int(5)  unsigned NOT NULL default '0', 
    duration int(5)  unsigned NOT NULL default '0', sip2dip smallint(5)  NOT NULL default '0', 
    -- the number of source IP to the same destination IP
    dip2sip smallint(5)  NOT NULL default '0',
    -- the number of destination IP to the same source port
    sportsip smallint(5)  NOT NULL default '0',
    sportdip smallint(5)  NOT NULL default '0',
    -- the number of source IP to the same destination port
    dportsip smallint(5)  NOT NULL default '0',
    dportdip smallint(5)  NOT NULL default '0', PRIMARY KEY  (rid)
    );  //