简体   繁体   中英

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.

  • Not all the idhash columns from table 2 have a corresponding idhash in table 1. Stiil store those 'mismatched' rows with empty username and usermail

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

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.

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.

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

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

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

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

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

I would do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM