【问题标题】:How to combine 2 tables into one?如何将两张表合二为一?
【发布时间】:2019-02-14 19:54:30
【问题描述】:

我有两张表 (A) 和 (B),在不同的日期包含几乎相同的信息。

表(A)

+----+------------+----------+-------+
|ID  | Date       | Status   | Amount|
+----+------------+----------+-------+
| A1 | 01/14/2011 | Received |15     |
| A2 | 01/18/2011 | Received |65     |
| T3 | 01/9/2011  | Received |85     |
| X7 | 02/16/2011 | Received |35     |
| A5 | 01/3/2011  | Received |10     |
+----+------------+----------+-------+

表(B)

+----+------------+----------+-------+
|ID  | Date       | Status   | Amount|
+----+------------+----------+-------+
| A1 | 03/31/2012 | Approved |15     |
| A2 | 03/31/2012 | Denied   |65     |
| A3 | 03/31/2012 | Approved |85     |
| A7 | 03/31/2012 | Received |35     |
| A5 | 03/31/2012 | pending  |10     |
+----+------------+----------+-------+

我正在尝试合并它们以获得这样的新表(结果):

表格(结果)

+----+------------+----------+-------+
|ID  | Date       | Status   | Amount|
+----+------------+----------+-------+
| A1 | 01/14/2011 | Received |15     |
| A1 | 03/31/2012 | Approved |15     |
| A2 | 01/18/2011 | Received |65     |
| A2 | 03/31/2012 | Denied   |65     |
| A5 | 01/3/2011  | Received |10     |
| A5 | 03/31/2012 | pending  |10     |
+----+------------+----------+-------+

我使用了这个代码:

SELECT *
FROM table1
JOIN table2
ON table1.id = table2.id
ORDER BY table1.id;

结果如下:

+----+------------+----------+-------+------------+------------+---------+
| ID | Date       | Status   | Amount| Date_1     | Status_1   | Amount_1|
+----+------------+----------+-------+------------+------------+---------+
| A1 | 01/14/2011 | Received |15     |03/31/2012  | Approved   |15       |
+----+------------+----------+-------+------------+------------+---------+

它工作正常,但不是我想要的。我不想并排重复列。我在考虑使用 INSERT INTO 或 UNION ALL,但我不确定。

【问题讨论】:

  • “像这样”是什么意思?请不要指望人们从一个例子中猜测,用词来说明一个例子是一个例子。

标签: sql oracle join merge


【解决方案1】:

使用 UNION 时,每个表中仅包含另一表中对应行的行:

SELECT * FROM TableA a
WHERE EXISTS (
  SELECT 1 FROM TableB
  WHERE ID = a.ID AND Amount = a.Amount AND Status <> a.Status
)
UNION
SELECT * FROM TableB b
WHERE EXISTS (
  SELECT 1 FROM TableA
  WHERE ID = b.ID AND Amount = b.Amount AND Status <> b.Status
)
ORDER BY ID;

我不确定Amount = x.Amount 的情况。也许你想删除它。

【讨论】:

    【解决方案2】:

    这是一个简单的联合所有查询来完成这项工作:

    select * from a where id in (select id from b)
    union all
    select * from b where id in (select id from a)
    order by id, date;
    

    【讨论】:

      【解决方案3】:

      试试这个:

      SELECT t2.*
      FROM table1 t1
      INNER JOIN 
      (
          --Get results from both tables
          SELECT *
          FROM table1
          UNION ALL
          SELECT *
          FROM table2
      ) t2
          on t1.ID = t2.ID
      ORDER BY t2.ID;
      

      【讨论】:

        【解决方案4】:
        select x.id, x.status, x.amount, y.status, y.amount
        from 
        (
        select 'A1' id, 'received' status, 15 amount from dual
        union all
        select 'A2' id, 'received' status, 65 amount from dual
        ) x,
         (
        select 'A1' id, 'approved' status, 15 amount from dual
        union all
        select 'A2' id, 'denied' status, 65 amount from dual
        ) y
        where x.id = y.id
        

        希望对你有帮助!

        【讨论】:

          【解决方案5】:

          如果目标(您还没有真正描述过)是报告两个表中都存在 ID 的表中的行,那么您可能想要这样:

          select id, somedate, status, amount
          from   ( select id, somedate, status, amount from table_a
                   union all
                   select id, somedate, status, amount from table_b )
          where  id in
                 ( select id from table_a
                   intersect
                   select id from table_b )
          

          【讨论】:

            【解决方案6】:

            使用 UNION:

            SELECT ID, Date, Status, Amount
            FROM table1
            UNION
            SELECT ID, Date, Status, Amount
            from table2
            ORDER BY ID;
            

            【讨论】:

            • 我一定误解了你想要的结果。如果一个表中有记录,而另一个表中没有记录,则您不需要它。
            猜你喜欢
            • 1970-01-01
            • 2012-10-21
            • 1970-01-01
            • 2019-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多