BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL keyword to find difference

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Alan
Guest





PostPosted: Thu Aug 05, 2004 5:53 am    Post subject: SQL keyword to find difference Reply with quote



UNION is the keyword to combine 2 dataset in SQL, what is the keyword to do
the opposite ?

I want to find out the data that exists in one table but not in the other,
for example,
Table 1: 1, John
2, Peter

Table 2: 1, Sam
2, Peter

I want to get the result is 1, John, ie, record in Table 1 but not in Table
2.
Note: I am using MySQL 4.0, it does not support subquery.


Back to top
Arno Brinkman
Guest





PostPosted: Thu Aug 05, 2004 7:28 am    Post subject: Re: SQL keyword to find difference Reply with quote



Hi,

Quote:
UNION is the keyword to combine 2 dataset in SQL, what is the keyword to do
the opposite ?

I want to find out the data that exists in one table but not in the other,
for example,
Table 1: 1, John
2, Peter

Table 2: 1, Sam
2, Peter

I want to get the result is 1, John, ie, record in Table 1 but not in Table
2.
Note: I am using MySQL 4.0, it does not support subquery.

I see that you have the same ID's, but different names in both tables is that correct?

SELECT
t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON (t2.ID = t1.ID and t2.Name = t1.Name)
WHERE
t2.ID IS NULL

Regards,
Arno Brinkman
ABVisie

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/

Support list for Interbase and Firebird users :
[email]firebird-support (AT) yahoogroups (DOT) com[/email]

Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info



Back to top
Daryl
Guest





PostPosted: Thu Aug 05, 2004 8:28 am    Post subject: Re: SQL keyword to find difference Reply with quote



Hi Alan
The word you are looking for is probably INTERSECTION. However it is not a
keyword with SQL server anyway.

From SQL Server on-line books.

Hope it helps...

Using EXISTS and NOT EXISTS to Find Intersection and Difference
Subqueries introduced with EXISTS and NOT EXISTS can be used for two
set-theory operations: intersection and difference. The intersection of two
sets contains all elements that belong to both of the original sets. The
difference contains elements that belong only to the first of the two sets.

The intersection of authors and publishers over the city column is the set
of cities in which both an author and a publisher are located.

USE pubs
SELECT DISTINCT city
FROM authors
WHERE EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
Here is the result set:

city
--------
Berkeley

(1 row(s) affected)
Of course, this query could be written as a simple join.

USE pubs
SELECT DISTINCT authors.city
FROM authors INNER JOIN publishers
ON authors.city = publishers.city
The difference between authors and publishers over the city column is the
set of cities where an author lives but no publisher is located, that is,
all the cities except Berkeley.

USE pubs
SELECT DISTINCT city
FROM authors
WHERE NOT EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
[snip]
Quote:
UNION is the keyword to combine 2 dataset in SQL, what is the keyword to
do
the opposite ?



Back to top
Ray Marron
Guest





PostPosted: Thu Aug 05, 2004 8:34 pm    Post subject: Re: SQL keyword to find difference Reply with quote

"Alan" <NOSPAMalan_pltse (AT) yahoo (DOT) com.au> wrote

Quote:
UNION is the keyword to combine 2 dataset in SQL,
what is the keyword to do the opposite ? [SNIP]

The keywords you're looking for are UNION, INTERSECT and EXCEPT.

Here's a link to the appropriate PostgreSQL documentation, which I believe
is standards compliant in this respect:

http://www.postgresql.org/docs/7.4/interactive/queries-union.html

--
Ray Marron



Back to top
Alan
Guest





PostPosted: Fri Aug 06, 2004 6:58 am    Post subject: Re: SQL keyword to find difference Reply with quote

Yes, it works.
Thank you.

Quote:
I see that you have the same ID's, but different names in both tables is
that correct?

SELECT
t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON (t2.ID = t1.ID and t2.Name = t1.Name)
WHERE
t2.ID IS NULL





Back to top
corey lawson
Guest





PostPosted: Tue Aug 10, 2004 9:22 pm    Post subject: Re: SQL keyword to find difference Reply with quote

"Alan" <NOSPAMalan_pltse (AT) yahoo (DOT) com.au> wrote in
news:4111cb5f$1 (AT) newsgroups (DOT) borland.com:

Quote:
UNION is the keyword to combine 2 dataset in SQL, what is the keyword
to do the opposite ?

I want to find out the data that exists in one table but not in the
other, for example,
Table 1: 1, John
2, Peter

Table 2: 1, Sam
2, Peter

I want to get the result is 1, John, ie, record in Table 1 but not in
Table 2.
Note: I am using MySQL 4.0, it does not support subquery.



Use a left outer join query, and select one of the fields in the "right"
side of the join that is NULL (i.e., not matched with the left table).

Or, use NOT EXISTS with a nested subquery.

Both are logically equivalent, but depending on the system, one will tend
to be much faster than the other...



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.