有个偏方哈哈。。把全部删除,再插一条仅供参考 DELECT FROM t_emp WHERE name='abc'; INSERT INTO t_emp VALUES ('abc');
如果id是数字,可以 delete from t_emp t where t.id not in (select min(m.id) from t_emp m group by m.name)
delete t_emp table where id not in( select min(t.id) from t_emp t group by t.id,t.name); 这样哪里错了 谢谢
貌似没有这样的语句撒,不过给楼主一个建议,把数据先倒出来,然后重新设计数据库, 一条一条的插入,当遇到已经存在的就不插入, 没有重复的就执行插入语句,简答的说就是:插入记录时检查记录是否已经存在,存在则更新,不存在则插入记录不知道您用的什么数据库, 思想是这样的: SELECT COUNT(*) FROM xxx WHERE ID=xxx; if (x == 0) INSERT INTO xxx VALUES; else UPDATE xxx SET ;给你举个例子 创建一张表用来存储用户: create table user_info ( uid mediumint(10) unsigned NOT NULL auto_increment primary key, last_name char(20) not null, first_name char(20) not null, unique ( last_name, first_name) ); alter table anser add UNIQUE (last_name,first_name) 插入数据: insert ignore into user_info (last_name,first_name) values ('x','y'); 这样一来,如果表中已经存在last_name='x'且first_name='y'的数据,就不会插入,如果没有就会插入一条新数据。具体的可以参看: http://blog.sina.com.cn/s/blog_68b4ec9b01014dy3.html
id是主键,每个id都不一样,作为group by的条件,和不作为条件没有区别。而且按你的意思,只是要把name值重复的数据,只保留一条,所以才要用name作为group by 条件。按我写的试一下,应该没问题吧。
就错了一点,Group By 中不应该有 t.id ! 因为你 t.id 是主键,用于Group By的话,就意味着所有t.id都会被选择出来。请修改为: delete t_emp table where id not in( select min(t.id) from t_emp t group by t.name );
delete from B where id not in( select min(t.id) from B t group by t.name);You can't specify target table 'B' for update in from clause这个应该怎么更新
居然不支持这种子查询,只好分成两步来处理了:1、把需要保留的数据写入临时表。 Select min(id) Into tmp_B From B Group By name;2、借助临时表来删除数据 delete from B where id not in( select t.id from tmp_B t );
delect from emp where rowid not in(select max(rowid) from emp rowid 是数据库的一个伪列
delete from tab where id in (select max(id) from tab A inner join tab B on A.id = B.id where A.name = B.name ) and name = '....'
DELECT FROM t_emp WHERE name='abc';
INSERT INTO t_emp VALUES ('abc');
delete from t_emp t where t.id not in (select min(m.id) from t_emp m group by m.name)
select min(t.id)
from t_emp t
group by t.id,t.name);
这样哪里错了 谢谢
一条一条的插入,当遇到已经存在的就不插入,
没有重复的就执行插入语句,简答的说就是:插入记录时检查记录是否已经存在,存在则更新,不存在则插入记录不知道您用的什么数据库,
思想是这样的:
SELECT COUNT(*) FROM xxx WHERE ID=xxx;
if (x == 0)
INSERT INTO xxx VALUES;
else
UPDATE xxx SET ;给你举个例子
创建一张表用来存储用户:
create table user_info
(
uid mediumint(10) unsigned NOT NULL auto_increment primary key,
last_name char(20) not null,
first_name char(20) not null,
unique ( last_name, first_name)
);
alter table anser add UNIQUE (last_name,first_name)
插入数据:
insert ignore into user_info (last_name,first_name) values ('x','y');
这样一来,如果表中已经存在last_name='x'且first_name='y'的数据,就不会插入,如果没有就会插入一条新数据。具体的可以参看:
http://blog.sina.com.cn/s/blog_68b4ec9b01014dy3.html
就错了一点,Group By 中不应该有 t.id !
因为你 t.id 是主键,用于Group By的话,就意味着所有t.id都会被选择出来。请修改为:
delete t_emp table where id not in(
select min(t.id)
from t_emp t
group by t.name
);
select min(t.id)
from B t
group by t.name);You can't specify target table 'B' for update in from clause这个应该怎么更新
Select min(id)
Into tmp_B
From B
Group By name;2、借助临时表来删除数据
delete from B where id not in(
select t.id
from tmp_B t
);
where id in (select max(id)
from tab A inner join tab B on A.id = B.id
where A.name = B.name
)
and name = '....'