Page 1 of 1

Merging Unique emails from 2 tables

Posted: Sun Aug 31, 2008 9:49 am
by kkonline
Hi community,
I have Table 1 and Table2 both having unique emails within them.

Table 1 being the main email Container, I want INTERSECTION of Table 1 and Table2 and the UNIQUE emails between the two should be moved to Table 1.

So now that Table 1 should have All the Unique Emails within itself and also between Table 1 and Table2


ex

Table 1
A
B
C
D

Table 2
F
G
I
B
C

So finally Table 1 should have
A
B
C
D
F
G
I

Note: values B and C already exist in Table 1 and should be ignored.

How to write this query?

Re: Merging Unique emails from 2 tables

Posted: Sun Aug 31, 2008 10:01 am
by marcth
Not very efficient and my not work under mysql.

Code: Select all

 
SELECT DISTINCT
  TABLE_A.EMAIL
FROM
  TABLE_A,
WHERE
  TABLE_A.EMAIL NOT IN (
    SELECT DISTINCT
      TABLE_B.EMAIL
    FROM
      TABLE_B
    )
)
UNION SELECT DISTINCT
  TABLE_B.EMAIL
FROM
  TABLE_B,
WHERE
  TABLE_B.EMAIL NOT IN (
    SELECT DISTINCT
      TABLE_A.EMAIL
    FROM
      TABLE_A
    )
)
 

Re: Merging Unique emails from 2 tables

Posted: Sun Aug 31, 2008 10:16 am
by kkonline
Hi,
the query

Code: Select all

(SELECT DISTINCT
   invites.email
 FROM
   invites,
 WHERE
   invites.email NOT IN (
     SELECT DISTINCT
       invites_test.email
     FROM
       invites_test
     )
 )
 UNION (SELECT DISTINCT
  invites_test.email
 FROM
   invites_test,
 WHERE
   invites_test.email NOT IN (
    SELECT DISTINCT
       invites.email
     FROM
      invites
     )
)
Doesn't seem to work

Re: Merging Unique emails from 2 tables

Posted: Sun Aug 31, 2008 10:32 am
by marcth
Try executing eveything before the union statement

Re: Merging Unique emails from 2 tables

Posted: Sun Aug 31, 2008 2:58 pm
by kkonline
Issue solved with the following code

Code: Select all

INSERT INTO invites
SELECT invites_test.*
FROM invites_test
LEFT OUTER JOIN invites
ON invites.email = invites_test.email
WHERE invites.email IS NULL
Thanks