数据库根据一个表确定另一个表

原先的题目大概是说有一个表要我们变成另一个表

表一是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

嗯,到此结束。

仅作为记录用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值