在同一张表中,本人想用A字段和B字段相等,C字段等于0的记录的D字段去更新C字段等于1的D字段,即把D字段为空的字段分别更新成5和6。sql该怎么写?
解决方案:20分
CREATE TABLE test.table1 ( A varchar(5) NOT NULL, B varchar(5) NOT NULL, C int(11) NOT NULL, D int(11) DEFAULT NULL ) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci; UPDATE table1 t1, (SELECT * FROM table1 WHERE C = 0) t2 SET t1.d = t2.d WHERE t1.C = 1 AND t1.A = t2.A AND t1.b = t2.b