主题:求高手写条SQL语句并讲解写出来的什么意思
kingzhm
[专家分:450] 发布于 2007-07-25 00:04:00
有一张表 tables 结构如下
id aa bb cc
1 11 111 a
2 22 222 b
3 11 111 c
4 22 111 d
5 22 222 e
将上面表中aa bb 相同的都去掉 并将全部字段显示出来
显示结果
id aa bb cc
1 11 111 a
2 22 222 b
4 22 111 d
第一条和第三条一样可以随便去掉一条,第2条和5条一样也是随便去掉一条
这样的语句怎么写啊 有几种方法写几种方法
回复列表 (共5个回复)
沙发
Haggard_doom [专家分:80] 发布于 2007-07-25 14:01:00
select distinct * from tables
这个是查询不重复的
板凳
ilovemountainking [专家分:3730] 发布于 2007-07-25 23:17:00
delete from test a
where a.id in
(select max(b.id)
from test b
group by (b.aa,b.bb)
having count(*) > 1)
3 楼
harvard [专家分:530] 发布于 2007-07-26 21:10:00
select *
from tables
where id not in
(
select R.id
from tables R,tables S
where R.id != S.id
and R.aa = S.aa
and R.cc = S.cc
)
4 楼
潇洒老乌龟 [专家分:1050] 发布于 2007-08-08 10:08:00
id aa bb cc
1 11 111 a
2 22 222 b
3 11 111 c
4 22 111 d
5 22 222 e
select * from tb a,
(select aa,bb,min(id) id from tb group by aa,bb) b
where a.aa = b.aa and a.bb = b.bb and a.id = b.id
5 楼
潇洒老乌龟 [专家分:1050] 发布于 2007-08-08 10:11:00
create table tb (id int,aa int,bb int,cc varchar(10))
insert into tb values(1, 11, 111, 'a')
insert into tb values(2, 22, 222, 'b')
insert into tb values(3, 11, 111, 'c')
insert into tb values(4, 22, 111, 'd')
insert into tb values(5, 22, 222, 'e')
select a.* from tb a,
(select aa,bb,min(id) id from tb group by aa,bb) b
where a.aa = b.aa and a.bb = b.bb and a.id = b.id
order by a.id
drop table tb
/*
id aa bb cc
----------- ----------- ----------- ----------
1 11 111 a
2 22 222 b
4 22 111 d
(所影响的行数为 3 行)
*/
我来回复