原先的题目大概是说有一个表要我们变成另一个表
表一是id 和score
表二是id 和score和state如果分数高于60分则state为pass否则为fail
由于题目不限方法,一开始我用的是Java的JDBC方法。后来觉得应该用SQL语言实现
首先是根据table1创建一个table2
mysql> create table table2(
-> id int(4) not null,
-> score int(4) not null,
-> state varchar(4) default null);//这里的default null是关键。
Query OK, 0 rows affected (0.11 sec)
紧接着把table1的内容复制到table2
mysql> insert into table2(id,score) select * from table1;
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
然后再更新table2:
mysql> update table2 set state='pass' where score>60;//注意没有from table2这个
Query OK, 3 rows affected (0.08 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> update table2 set state='fail' where score<60;
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0
嗯,到此结束。
仅作为记录用。
本文介绍了一种使用SQL语言将原始表中的数据转换并增加状态字段的过程。通过创建新表、复制数据及条件更新实现了根据分数自动标记状态的功能。
3263

被折叠的 条评论
为什么被折叠?



