Click to See Complete Forum and Search --> : Multiple SQL Tables


kier
01-20-2006, 10:45 AM
I am trying to pull data from a table (table A), but only want to pull the records where the same ID does not appear in a second table (table B).

Is this possible? Table A contains a unique ID field, which is referenced in Table B, so this field can be used to find entries for the same person.

I started with just pulling the info I wanted from one table:

SELECT * FROM A WHERE (availability = 'All') AND (site = '" & site & "') OR (availability = '" & availability & "') AND (site = '" & site & "')"

and now think I need an "AND B.ID field ....." But that is where I get lost!

I'm sure this is a very simple piece of code!
Thank you for any assistance you can provide!

chazzy
01-20-2006, 11:25 AM
INSERT INTO B SELECT * FROM A WHERE A.ID (IS) NOT IN (SELECT ID FROM B)


the syntax for IS NOT IN will vary based on your DBMS. try it in various forms (IS NOT IN, NOT IN, ETC)

chrismartz
01-20-2006, 05:49 PM
Or try,SELECT * FROM A, B WHERE A.ID <> B.ID

Cstick
01-20-2006, 08:29 PM
SELECT
*
FROM
Table1 A
WHERE
(SELECT COUNT(*) FROM Table2 B WHERE B.ID = A.ID) = 0