Merlin
Merlin

Reputation: 25709

Combining two table without duplicates

I have a table containing symbols:

Table1

'sym'
Ibm
Msft
SUnw

Table 2

'sym'
ABC
BCD
CDE
IBM

Using mysql: how could add the unique 'sym' from table 2 into table 1.

Upvotes: 1

Views: 2170

Answers (2)

Tim
Tim

Reputation: 5421

You can use the "not exists" test:

       insert t1
       (sym)
       select sym from T2 where not exists
       (select sym from T1 where T1.sym = T2.sym)

Upvotes: 1

Andomar
Andomar

Reputation: 238296

You could use distinct, and add a not exists condition to filter out the symbols already in Table1:

insert  Table1
        (sym)
select  distinct sym
from    Table2
where   not exists
        (
        select  *
        from    Table1
        where   sym = Table2.sym
        )

Upvotes: 4

Related Questions