两个表中的关联字段赋值问题

spear_nj 2002-10-15 07:41:57
A表
ID 姓名 学历
001 张山 大学
002 李氏 硕士

B表
ID 学校 文化程度
001 清华 大学
001 北大 硕士
002 南大 大学
002 南大 硕士
我想把B表中同ID的最高文化程度的值赋给A表相应的字段
例如把B表中的第二行纪录里的文化程度来更新A表中的001的学历
update A set 学历=(select 文化程度 from B group by id having MAX(文化程度)) where A.ID=B.ID
我这样写对不对?我目前没有测试机器,请大家帮个忙!





...全文
97 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
spear_nj 2002-10-16
  • 打赏
  • 举报
回复
update A set 学历=(select MAX(文化程度) from B where B.ID=A.ID);
这样是不是能把ID相同的最高文化程度 赋给A表对应的字段?
还想加一个约束
update A set 学历=(select MAX(文化程度) from B where B.ID=A.ID and B.学校 is not null);

zhaoyongzhu 2002-10-16
  • 打赏
  • 举报
回复
数据库设计的极其不合理!!!
joe_moonrush 2002-10-16
  • 打赏
  • 举报
回复
update A set 学历=(select MAX(文化程度) from B where B.ID=A.ID);
就ok了
yuxuan 2002-10-16
  • 打赏
  • 举报
回复
我看最好用存储过程实现比较好
spear_nj 2002-10-16
  • 打赏
  • 举报
回复
这样写呢?
update A set 学历=(select MAX(文化程度) from B group by id where 学校 is not null) where A.ID=B.ID
spear_nj 2002-10-16
  • 打赏
  • 举报
回复
A表
ID 姓名 学历
001 张山 大学
002 李氏 硕士

B表
ID 学校 文化程度
001 大学
001 北大 硕士
002 南大 大学
002 南大 硕士
update A set 学历=(select 文化程度 from B group by id having MAX(文化程度) where 学校 is not null) where A.ID=B.ID
我想实现上面这句的功能,语句应该怎么写?上面的句子是错误的。
spear_nj 2002-10-15
  • 打赏
  • 举报
回复
关键是先要把B表分组,然后把同组的最高的文化程度的纪录更新到A表
同时要判断B表的纪录中文化程度不为空
update A set 学历=(select 文化程度 from B group by id having MAX(文化程度) where 文化程度 is not null) where A.ID=B.ID
意思就是这样
请高手帮忙

tanjun_007 2002-10-15
  • 打赏
  • 举报
回复
关键是先把B表中每一ID的最高文化程度选出来
Select ID,MAX(文化程度) as 文化程度 from B group by ID
再更新A表。
google_real 2002-10-15
  • 打赏
  • 举报
回复
sorry,i mean store educational level code to the column 文化程度,
google_real 2002-10-15
  • 打赏
  • 举报
回复
store educational level to the column 文化程度,and then

update A set 学历=nvl((select max(文化程度) from B
where A.ID=B.ID),"<default educational level>")
prileng 2002-10-15
  • 打赏
  • 举报
回复
update A set 学历=(select MAX(文化程度) from B where B.ID=A.ID);
jiezhi 2002-10-15
  • 打赏
  • 举报
回复
MAX(文化程度)肯定是不对的。
再创建一个学位表,把学位按高低排。


17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧