主要是要理解group by 语句的作用。
group by 语句用来对行数据进行分组,故,需要查询的列必须都被包含在分组的列中,可少不可多。
如下删除表 t_temp中与content列重复的数据:
思路是先找出不需删除的id,然后外面套用一个not in 语句进行删除。
不需删除的id,使用group by 语句。
group by 语句用来对行数据进行分组,故,需要查询的列必须都被包含在分组的列中,可少不可多。
如下删除表 t_temp中与content列重复的数据:
思路是先找出不需删除的id,然后外面套用一个not in 语句进行删除。
不需删除的id,使用group by 语句。
create table t_temp(uno integer not null unique, content varchar2(6));
select * from t_temp;
UNO CONTENT
---------- ------------
1 a
2 a
3 b
4 b
5 b
6 d
7 d
8 d
9 e
已选择9行。
select min(uno),content
from t_temp
group by content;
MIN(UNO) CONTENT
---------- ------------
1 a
3 b
6 d
9 e
delete from t_temp
where uno not in(
select min(uno)
from t_temp
group by content
);
已删除5行。
select * from t_temp;
UNO CONTENT
---------- ------------
1 a
3 b
6 d
9 e