如何使用mysql中的临时表更新表

时间:2014-03-12 09:46:25

标签: mysql

CREATE TEMPORARY TABLE IF NOT EXISTS ES3( 
    Select T148001,T148002,T148003,T148006  
    From T148 where T148008 = Monthy and T148009 = Yeary and T148007 = ID 
); 

update SalaryTable , ES3 src 
Set  Present = T148003, WorkingDay = T148002,  Overtime = T148006    
Where Empid = T148001 ;

1 个答案:

答案 0 :(得分:0)

示例代码如下:

CREATE TEMPORARY TABLE Groups (Id INTEGER AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(31), GroupId VARCHAR(31), ValWithinGroup INTEGER);

INSERT INTO Groups VALUES (NULL, "Davy", "Boy", 2);
INSERT INTO Groups VALUES (NULL, "Mary", "Girl", 2);
INSERT INTO Groups VALUES (NULL, "Bill", "Boy", 5);
INSERT INTO Groups VALUES (NULL, "Jill", "Girl", -3);
INSERT INTO Groups VALUES (NULL, "Fred", "Boy", 3);

#查找每个组的最低值

CREATE TEMPORARY TABLE GroupSum AS SELECT GroupId, MIN(ValWithinGroup)
AS baseVal FROM Groups GROUP BY GroupId;

#创建一个索引,以便mySQL可以高效匹配

ALTER TABLE GroupSum ADD UNIQUE(GroupId);

#finally,进行基线调整

UPDATE Groups LEFT JOIN GroupSum USING (GroupId)
SET ValWithinGroup=ValWithinGroup-baseVal;
SELECT * FROM Groups;