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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Exclude table from SELECT

Author  Topic 

djavet
Starting Member

36 Posts

Posted - 2005-03-18 : 10:39:26
Hello,

I've a query wich with I select buildings form a table.
I've a second table with a few building I don't want in my select (exclude form select).
How can I make this?

Thx a lot.
Dom

PS: I'm a sql newbie...

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-03-18 : 10:41:27
Just don't list the ones you don't want to select.

Semper fi,

XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!
Go to Top of Page

djavet
Starting Member

36 Posts

Posted - 2005-03-18 : 10:45:52
I know that, but how? If I've 2 tables...

Regards, Dom

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-03-18 : 10:56:34
SELECT * FROM Table1 o
WHERE NOT Exists (SELECT * FROM Table2 i where i.building = o.building)

Brett

8-)
Go to Top of Page

Xerxes
Aged Yak Warrior

666 Posts

Posted - 2005-03-18 : 11:12:36
Sorry, Dom. Obviously I misunderstood you. Perhaps if you had provided a little more information, or maybe an example then I would have been able to better answer your question.

My apologies.

Semper fi,

XERXES, USMC(Ret.)
------------------------------------------------------
The Marine Corps taught me everything but SQL!
Go to Top of Page

Blastrix
Posting Yak Master

208 Posts

Posted - 2005-03-18 : 12:02:48
quote:
Originally posted by X002548

SELECT * FROM Table1 o
WHERE NOT Exists (SELECT * FROM Table2 i where i.building = o.building)




Brett(or anyone),
Just curious, is it better to do the way you listed, or to do one of the following? They all obtain the same results. Just in different ways.

SELECT *
FROM Table1 o
LEFT JOIN Table2 i
ON o.building = i.building
WHERE i.building IS NULL

or possibly

SELECT *
FROM Table1 o
WHERE o.building NOT IN (SELECT building FROM Table2)

Steve
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-18 : 12:05:04
there was already a discussion on that.
i think the results were:
for large tables
1. left join
2. not exists
3. not in

for small tables
1. not in
2. not exists
3. left join



Go with the flow & have fun! Else fight the flow
Go to Top of Page

djavet
Starting Member

36 Posts

Posted - 2005-03-22 : 02:10:13
Thx a lot!

Dom
Go to Top of Page
   

- Advertisement -