【SQL】判断后写入另一个表

来源:百度知道 编辑:UC知道 时间:2024/05/27 15:58:55
表一 inventory
pcid ipaddress
3 10.189.190.1
4 10.189.190.8
5 10.189.52.15
7 10.189.51.71
8 10.189.66.21
11 10.189.88.22

表二 ipdept
pcid department
11 OSS

将表一中的IP选范围,判断IP所属部门添加到表二

例如
如果是10.189.190.0到10.189.190.255,就把pcid添加到表二ipdept,并在department列添加HR
如果是10.189.51.0到10.189.66.0,就把pcid添加到表二ipdept,并在department列添加M2M
等等

如遇到两个表的pcid相同,就不添加到表二,也就是说pcid是唯一值。

select * from inventory
where ip_address between '10.189.190.0' and '10.189.190.255'
然后怎么写,能不能写个IP和部门的对照字典?
添加到表二是用insert into还是update?

最好有具体语句
判断倒是知道怎么写了
case when xxxx then xxxx

问题是 ip怎么进行比较啊 假如between '10.189.190.1' and '10.189.190.60' 那么10.189.190.255 也在这个范围内 60被认为成600000无限个零,怎么设置数据格式呢

表二 ipdept 是张空表,只要保证表一 inventory 中的pcid不重,between 包含的范围不重,插到ipdept表中的记录肯定不会重复.

如果表二ipdept已经有记录,那先插入后,最后根据情况虑重,留下你需要的

insert into ipdept
select pcid,'OSS' from inventory
where ip_address between '10.189.190.0' and '10.189.190.255'