如何将具有不同列的两个表联合起来

时间:2016-02-17 17:30:02

标签: sql

我有两个名为Table_A和Table_B的表,如下所示。

表-A

pod 'GoogleSignIn'
pod 'GoogleAnalytics'

表-B

+-------+------------+------+---------+-----+
|MonthId|DepartmentId|Salary|Deduction| Paid|
+-------+------------+------+---------+-----+
|01/2016|    DEP01   | 1000 |     100 |  900|
|01/2016|    DEP02   | 2000 |      50 | 1950|
|01/2016|    DEP03   | 1500 |       0 | 1500|
|       |            |      |         |     |
|02/2016|    DEP01   | 1000 |     100 |  900|
|02/2016|    DEP02   | 2000 |      50 | 1950|
|02/2016|    DEP03   | 1500 |       0 | 1500|
+-------------------------------------------+

所以当我尝试

+-------+------------+------+---------+-----+------+------+
|MonthId|DepartmentId|Salary|Deduction| Paid|Bank_1|Bank_2|
+-------+------------+------+---------+-----+------+------+
|01/2016|    DEP01   | 1000 |     100 |  900|  400 |  500 |
|01/2016|    DEP02   | 2000 |      50 | 1950| 1400 |  550 |
|01/2016|    DEP03   | 1500 |       0 | 1500|    0 | 1500 |
+---------------------------------------------------------+
  

WHERE MonthId = '02 / 2016'

我得到了我需要的东西但是如果尝试用

  

WHERE MonthId = '01 / 2016'

我得到轰鸣声结果。所以请帮我解决这个问题。

SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Null AS Bank_1, Null AS Bank_2
FROM Table_A 
WHERE MonthId = '02/2016'

UNION

SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Bank_1, Bank_2
FROM Table_B 
WHERE MonthId = '02/2016'

3 个答案:

答案 0 :(得分:0)

我怀疑你想聚合:

SELECT MonthId, DepartmentId, Salary, Deduction, Paid, MAX(Bank_1), MAX(Bank_2)
FROM (SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Null AS Bank_1, Null AS Bank_2
      FROM Table_A 
      WHERE MonthId = '02/2016'
      UNION ALL
      SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Bank_1, Bank_2
      FROM Table_B 
      WHERE MonthId = '02/2016'
     ) ab
GROUP BY MonthId, DepartmentId, Salary, Deduction, Paid;

答案 1 :(得分:0)

我认为LEFT JOIN应该这样做:

示例数据:

    CREATE TABLE #Table_A (MonthId VARCHAR(10), DepartmentId varchar(10), Salary int ,Deduction int, Paid int)
INSERT INTO #Table_A
VALUES
('01/2016','DEP01','1000','100','900'),
('01/2016','DEP02','2000','50','1950'),
('01/2016','DEP03','1500','0','1500'),
('02/2016','DEP01','1000','100','900'),
('02/2016','DEP02','2000','50','1950'),
('02/2016','DEP03','1500','0','1500')

CREATE TABLE #Table_B (MonthId  VARCHAR(10), DepartmentId varchar(10), Salary int ,Deduction int, Paid int, Bank_1 int ,Bank_2 int )
INSERT INTO #Table_B
VALUES
('01/2016','DEP01','1000','100','900','400','500'),
('01/2016','DEP02','2000','50','1950','1400','550'),
('01/2016','DEP03','1500','0','1500','0','1500')

使用WHERE子句进行查询:

SELECT  A.MonthId, 
       A.DepartmentId, 
       A.Salary, 
       A.Deduction, 
       A.Paid, 
       B.Bank_1, 
       B.Bank_2
FROM       #Table_A AS A LEFT OUTER JOIN 
       #Table_B AS B ON A.MonthId = B.MonthId AND 
                    A.DepartmentId = B.DepartmentId AND 
                    A.Salary = B.Salary AND 
                    A.Deduction = B.Deduction AND 
                    A.Paid = B.Paid
WHERE A.MonthId = '01/2016'

结果:

enter image description here

不带WHERE子句的查询:

SELECT  A.MonthId, 
       A.DepartmentId, 
       A.Salary, 
       A.Deduction, 
       A.Paid, 
       B.Bank_1, 
       B.Bank_2
FROM       #Table_A AS A LEFT OUTER JOIN 
       #Table_B AS B ON A.MonthId = B.MonthId AND 
                    A.DepartmentId = B.DepartmentId AND 
                    A.Salary = B.Salary AND 
                    A.Deduction = B.Deduction AND 
                    A.Paid = B.Paid

结果(因为在#Table_B中没有该MonthId的记录,因此在Bank_1中没有任何值,因为02/2016没有Bank_2):

enter image description here

答案 2 :(得分:0)

看起来你应该使用LEFT OUTER JOIN而不是union,如果我猜测你想要table1中的所有值,如果它们存在,则使用Bank_1和Bank_2值。这应该有效:

&