标签:
(在Mysql数据库上面进行测试)
create table a (ID int,Name char(20));
create table b (ID int,Name char(20));
1.当b表存在a表相同的ID字段的时候,将a表的Name更新到b表的Name。
update b,a set b.Name=a.Name where a.ID=b.ID;
2.当b表不存在a表ID的记录时,将a表的记录插入到b表中
insert into b select * from a where a.ID not in(select id from B);
3.去掉表a中重复的Name的记录。
delete from a where a.name in (select b.name from(select d.name from a d group by d.name having count(d.name)>1)b) and a.id not in (select id from (select max(c.id) id from a c group by c.name having count(c.name)>1)f);
这里select d.name from a d group by d.name having count(d.name)>1改成(select b.name from(select d.name from a d group by d.name having count(d.name)>1)b) 的原因是Mysql数据库,如果不改成这样会出现错误:
1093 - You can‘t specify target table ‘a‘ for update in FROM clause。
文档“Currently, you cannot update a table and select from the same table in a subquery.”
数据库这样子设计性能会降低吧?
4.设置表a的ID字段为主键
alter table a add primary key(ID);
删除主键alter table a drop primary key(ID);
5.在表a添加ReMark描述字段
alter table a add ReMark char(50);
6.
标签:
原文地址:http://www.cnblogs.com/avaj/p/5551210.html