在mysql视图中使用变量递增函数

时间:2017-03-17 13:39:08

标签: mysql

我使用以下查询创建了一个视图:

CREATE 
    VIEW `localmysql`.`view_system_property` AS
    SELECT 
        `t`.`ID` as `ID`,
        `t`.`PROPERTY_NAME` AS `PROPERTY_NAME`,
        `t`.`PROP_VALUE` AS `PROP_VALUE`,
        `t`.`PROJECT_GROUP_CODE` AS `PROJECT_GROUP_CODE`,
    FROM
        (SELECT func_inc_id() as `ID`,
            `sp`.`PROPERTY_NAME` AS `PROPERTY_NAME`,
                `sp`.`PROP_VALUE` AS `PROP_VALUE`,
                `sp`.`PROJECT_GROUP_CODE` AS `PROJECT_GROUP_CODE`,
                `sp`.`PROJECT_CODE` AS `PROJECT_CODE`,
        FROM
            `localmysql`.`system_property` `sp` 
        UNION 
        SELECT func_inc_id() as `ID`,
            `sps`.`PROPERTY_NAME` AS `PROPERTY_NAME`,
                `sps`.`PROP_VALUE` AS `PROP_VALUE`,
                `sps`.`PROJECT_GROUP_CODE` AS `PROJECT_GROUP_CODE`,
                `sps`.`PROJECT_CODE` AS `PROJECT_CODE`,
        FROM
            `localmysql`.`system_property_secured` `sps`) `t`;

使用的函数创建如下:

CREATE FUNCTION `func_inc_id`() RETURNS int
    NO SQL
    NOT DETERMINISTIC
     begin
      SET @var := @var + 1;
      return @var;
     end
     //

然后我将变量值设置为SET @var=0;

每次我执行select * from view_system_property ;时,id列的值都会递增。例如,如果整个视图有100行..第一次当我做一个select * id的值从1开始到100时,第二次调用给我从101到200的id等等......

问题是视图应该有一组固定的记录,为什么在执行select * from时值会增加?视图已经创建,那么为什么在调用视图时值会发生变化

0 个答案:

没有答案