SuperSpy
SuperSpy

Reputation: 1314

How do I merge two tables in SQL?

I've got two tables:

Table 1 = idhash - username - usermail
Table 2 = idhash - views    - fistseen - lastseen

Now I want to merge these tables to a new table:

Table 3 = idhash - username - usermail - firstseen - lastseen

*notice that I want to drop the views column.

I've tried with solutions that I found on google, but so far they do not seem to work.

Upvotes: 1

Views: 282

Answers (9)

Marcus Adams
Marcus Adams

Reputation: 53830

Here's a solution for MySQL:

CREATE TABLE table3
// First get all the rows from table2, paired with matching rows from table1
(SELECT a.idhash, b.username, b.usermail, a.firstseen, a.lastseen
FROM table2 a
LEFT JOIN table1 b
  ON b.idhash = a.idhash)
// Now get the remaining rows from table1 that don't have matches
UNION ALL
(SELECT null, a.username, a.usermail, null, null
FROM table1 a
LEFT JOIN table2 b
  ON b.idhash = a.idhash
WHERE b.idhash IS NULL)

If you don't want the rows from table1 that don't have corresponding rows in table2, then delete the second query in the UNION clause.

Upvotes: 1

Benoit
Benoit

Reputation: 79165

I would do this:

           CREATE TABLE table3
                     AS
                 SELECT idhash, username, usermail, firstseen, lastseen
                   FROM Table1
NATURAL FULL OUTER JOIN Table2

Upvotes: 0

John Woo
John Woo

Reputation: 263693

try this:

INSERT INTO table3 (idhash, username, usermail, firstseen, lastseen)
SELECT  a.idhash, a.username, a.usermail,
        b.firstseen, b.lastseen
FROM    table1 a LEFT JOIN table2 b 
            ON a.idhash = b.idhash

Upvotes: 0

Lamak
Lamak

Reputation: 70638

This query will give you the results:

SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash

And to insert and update the results on your [Table 3]:

INSERT INTO [Table 3](idhash, username, usermail, firstseen, lastseen)
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
LEFT JOIN [Table 3] C
ON A.idhash = C.idhash
WHERE C.idhash IS NULL

-- For SQL Server
UPDATE T3
SET firstseen = T1.firstseen,
    lastseen = T1.lastseen
FROM [Table 3] T3
INNER JOIN (SELECT A.*, B.firstseen, B.lastseen
            FROM [Table 1] A
            LEFT JOIN [Table 2] B
            ON A.idhash = B.idhash) T1
WHERE T3.firstseen != T1.firstseen OR T3.lastseen != T1.lastseen

Upvotes: 3

Vinnie
Vinnie

Reputation: 3929

This is pretty close to brute force.

select coalesce(t1.idhash, t2.idhash) as idhash
, username
, usermail
, firstseen
, lastseen
into table3
from table1 t1
cross join table2 t2

Upvotes: 0

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Create your new table with the field types in your create statement

http://www.w3schools.com/sql/sql_create_table.asp

Then do an insert Into Table3 <yourtable> select a.f1,b.f2,c.f3 from Table1 a, Table 2 b on a.id = b.id

Upvotes: 0

Rozwel
Rozwel

Reputation: 2020

This should be a good start. You need to tell us what to do with your mis-matched records before we can give anything more specific.

Select 
    table1.idhash
    ,username
    ,usermail
    ,firstseen
    ,lastseen
From table1
    left join table2
      on table1.idhash = table2.idhash

Upvotes: 0

Diego
Diego

Reputation: 36126

insert into table3
select t1.idhash, t1.username, t1.usermail, t2.firstseen,t2.lastseen
from table1 t1 left join table2 t2 on t1.idhas=t2.idhas

Upvotes: 0

kand
kand

Reputation: 2338

You could just brute force it with your choice of programming language. Just build a new table, query both tables, join rows programmatically however they need to be joined, insert into new table.

Upvotes: 0

Related Questions