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

求Query

有这样一张表
create table DBTest (OID int,Oname nvarchar(100),PID int,Com nvarchar(100))
insert into DBTest values ('12345','ABC','364','CZZXXCC')
insert into DBTest values ('12346','ABC','364','EZZXXEE')
insert into DBTest values ('12350','DFGFG','362','CCZZXXDD')
insert into DBTest values ('12351','DFGFG','365','EEZZXXFF')
insert into DBTest values ('12355','XXCVD','362','WWZZXXD')
insert into DBTest values ('12356','XXCVD','364','GGZZXXV')


如何Oname一样,但PID不一样的OID来。


------解决方案--------------------

SELECT a.* FROM dbtest a INNER JOIN dbtest b ON a.oname=b.oname
WHERE a.pid<>b.pid

/*
OID         Oname                                                                                                PID         Com
----------- ---------------------------------------------------------------- ----------- ----------------------------------------------------------------
12351       DFGFG                                                                                                365         EEZZXXFF
12350       DFGFG                                                                                                362         CCZZXXDD
12356       XXCVD                                                                                                364         GGZZXXV
12355&nbs