如何从MySQL数据库表中选择行时,使特定列值的第一行为空

时间:2014-03-18 08:48:32

标签: mysql

注意:我想make空第一行TParent_Group_ID为零或null,同时从数据库表中选择行

BEGIN

    DECLARE done INT DEFAULT 0;
    DECLARE next_id INT;
    -- CURSOR TO LOOP THROUGH RESULTS --
    DECLARE cur1 CURSOR FOR SELECT GID FROM civicsoft_group WHERE Parent_Group_ID = GivenID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    -- CREATE A TEMPORARY TABLE TO HOLD RESULTS --
    IF initial=1 THEN
        -- MAKE SURE TABLE DOESN'T CONTAIN OUTDATED INFO IF IT EXISTS (USUALLY ON ERROR) --
        DROP TABLE IF EXISTS OUT_TEMP; 
        CREATE TEMPORARY TABLE OUT_TEMP (TGID int,TParent_Group_ID int, TGroup_Name varchar(32),TGroup_Type varchar(1),TProject_Status tinyint(1),TEID int,TEmp_Group_ID int,TEmp_Name varchar(100),TEmp_Type varchar(2));
    END IF;

    -- ADD OURSELF TO THE TEMPORARY TABLE --
    INSERT INTO OUT_TEMP SELECT cg.GID,cg.Parent_Group_ID,cg.Group_Name,cg.Group_Type,cg.Project_Status,ce.EID,ce.Emp_Group_ID,ce.Emp_Name,ce.Emp_Type FROM civicsoft_group cg LEFT JOIN civicsoft_employee ce ON cg.GID = ce.Emp_Group_ID  WHERE cg.GID = GivenID;

    -- AND LOOP THROUGH THE CURSOR --
    OPEN cur1;
    read_loop: LOOP
        FETCH cur1 INTO next_id;

        -- NO ROWS FOUND, LEAVE LOOP --
        IF done THEN
        LEAVE read_loop;
        END IF;

        -- NEXT ROUND --

    SET @@GLOBAL.max_sp_recursion_depth = 255;
    SET @@session.max_sp_recursion_depth = 255; 

        CALL Hierarchy(next_id, 0);     
    END LOOP;

    CLOSE cur1;

    -- THIS IS THE INITIAL CALL, LET'S GET THE RESULTS --
    IF initial=1 THEN



         SELECT * FROM OUT_TEMP;  -- Producing Final Temporary Table output

        -- CLEAN UP AFTER OURSELVES --
        DROP TABLE OUT_TEMP; 
    END IF;
END

1 个答案:

答案 0 :(得分:1)

创建特殊第一行的一般解决方案是:

select 0, null, null, ...
union all
select *
from ...
where ...

如果列为第二行,则第一行必须具有完全相同的数字和类型。您可以为第一行选择您喜欢的任何特殊值,只要编码的值与列类型兼容即可。