Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 find pnid

Author  Topic 

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:03:06
create table t1 (pnid int, group_pn int)
insert into t1 values (1, 48)
insert into t1 values (2, 49)

create table t2 (pnid int, group_pn int)
insert into t2 values (3, 48)
insert into t2 values (4, 50)

i need to find pnid where the group_pn is not in t2

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-11 : 10:07:51
select t1.pnid from t1 T where not exists
(select * from t2 where pnid=T.pnid)

Also learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:08:36
thank u so much for your help
Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:10:46
but it doesnt work
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-11 : 10:12:26
quote:
Originally posted by gongxia649

but it doesnt work


Did you get error?
Did you get wrong result?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:13:35

(1 row(s) affected)


(1 row(s) affected)

Server: Msg 107, Level 16, State 3, Line 10
The column prefix 't1' does not match with a table name or alias name used in the query.
Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:14:14
i actally tried that way before. but it doesnt work.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-11 : 10:18:14
Well.
Try this

select T.pnid from t1 T where not exists
(select * from t2 where pnid=T.pnid)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2006-04-11 : 10:22:20
its showing 1 and 2. It should show just 2.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-11 : 10:42:20
Well. Ignore my previous answers
This should do what you want

select T.pnid from t1 T where not exists
(select * from t2 where group_pn=T.group_pn)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -