简体   繁体   English

如何在SQL中合并两个表?

[英]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. 我已经尝试过在Google上找到的解决方案,但到目前为止它们似乎没有用。

  • 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 并非表2中的所有idhash列在表1中都有对应的idhash。Stiil用空的用户名和用户邮件存储那些“不匹配”的行

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]: 并在[表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: 这是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. 如果您不希望table1中的行在table2中没有对应的行,则删除UNION子句中的第二个查询。

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 使用create语句中的字段类型创建新表

http://www.w3schools.com/sql/sql_create_table.asp 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 然后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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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