On 4 Apr 2005 08:48:30 -0700, chudson007 (AT) hotmail (DOT) com wrote:
Quote:
I want help with a couple of SQL queries.
I have two Tables Table A and Table B.
Both tables have the same two fields Name and Hobbies.
One Name can appear beside multiple hobbies in each table.
There are three queries I wish to run.
1) Find all the Hobbie and Name combinations in Table B not in Table A
only for Names that exist in Table A
2) Find all Hobbies and Name combinations in Table A not in Table B
3) Return all data in Table B that contains a Name that exists in Table
A
Regards,
Ciarán |
Hi Ciarán,
I guess the real question is why you store the same data in two tables.
Try if the following work. If not, then post table structure and sample
data as described here: www.aspfaq.com/5006.
1)
SELECT b.Hobbie, b.Name
FROM TableB AS b
WHERE EXISTS (SELECT *
FROM TableA AS a
WHERE a.Name = b.Name)
2)
SELECT a.Hobbie, a.Name
FROM TableA AS a
WHERE NOT EXISTS (SELECT *
FROM TableB AS b
WHERE b.Name = a.Name
AND b.Hobbie = a.Hobbie)
3) Same as 1.
Notes:
* 1 and 3 can also be done with an inner join, but you need some way to
prevent duplicates - either DISTINCT or a derived table
* 2 can also be done with an outer join.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)