新手问题,给讲一下哈!
tb1
id name startamount endamount date
1 a 10 20 2007-09-09
2 b 11 25 2007-09-09
3 c 15 65 2007-09-09
-------------
n * 21 44 2007-09-09
1 a 22 20 2007-09-10
2 b 55 66 2007-09-10
3 c 89 77 2007-09-10
------------
n * 34 30 2007-09-10
要求让9月9号的endamount赋给9月10号的startamount,9月10号的startamount不要了,怎么做?先谢谢哈!
[解决办法]
CREATE TABLE tb1(id INT,name VARCHAR(10), startamount INT, endamount INT, date DATETIME)
INSERT TB1
SELECT 1 , 'a ', 10 , 20 , '2007-09-09 '
UNION SELECT 2 , 'b ', 11 , 25 , '2007-09-09 '
UNION SELECT 3 , 'c ', 15 , 65 , '2007-09-09 '
UNION SELECT 1 , 'a ', 22 , 20 , '2007-09-10 '
UNION SELECT 2 , 'b ', 55 , 66 , '2007-09-10 '
UNION SELECT 3 , 'c ', 89 , 77 , '2007-09-10 '
UPDATE A
SET A.startamount=B.endamount
FROM TB1 A INNER JOIN (SELECT * FROM TB1 WHERE [date]= '2007-09-09 ') B ON A.ID=B.ID
WHERE A.[date]= '2007-09-10 '
SELECT * FROM TB1 ORDER BY DATE,ID
DROP TABLE TB1
/* 结果
id name startamount endamount date
----------- ---------- ----------- ----------- -----------------------
1 a 10 20 2007-09-09 00:00:00.000
2 b 11 25 2007-09-09 00:00:00.000
3 c 15 65 2007-09-09 00:00:00.000
1 a 20 20 2007-09-10 00:00:00.000
2 b 25 66 2007-09-10 00:00:00.000
3 c 65 77 2007-09-10 00:00:00.000
(6 row(s) affected)
*/