日期:2014-05-17  浏览次数:20771 次

问一个oracle的问题,某列的值如果相同只返回一条记录?这样的oracle怎么写?
比如查询出来的oracle结果有2列,1列为id,1列为name,现在要求如果name的值一样就返回一条记录?这样的oracle语句怎么写?

select t.id,t.name from t_student t;

------解决方案--------------------
示例如下:
create table q(
no varchar2(10),
jg varchar2(10));

insert into q values('q','10');
insert into q values('w','11');
insert into q values('e','10');
commit;

找出jg重复的
select q1.* from q q1,q q2 where q1.jg=q2.jg and q1.no<>q2.no
NO JG
---------- ----------
e 10
q 10

找出jg不重复的
select q.* from q where no not in (select q1.no from q q1,q q2 where q1.jg=q2.jg and q1.no<>q2.no)
NO JG
---------- ----------
w 11

在JG重复的中找出rownum=1的,并且和jg不重复的合在一起
select q.* from q where no not in (select q1.no from q q1,q q2 where q1.jg=q2.jg and q1.no<>q2.no)
union
select n,j from (select q1.no n,q1.jg j,rownum r from q q1,q q2 where q1.jg=q2.jg and q1.no<>q2.no) where r=1
NO JG
---------- ----------
e 10
w 11


------解决方案--------------------
select max(id) id,name from t_student group by name