在SQL Server 2005中添加两个具有一个匹配列名称的不同表

时间:2015-08-31 20:53:30

标签: sql sql-server

我的表格是这样的:

#table 1

+---------------------------------------------+
| name     Tpens Tpencils Tbooks              |
+---------------------------------------------+
| suresh      1       2        5              |
| ramesh      3       5        7              |
| ravi        2       7        9              |
+---------------------------------------------+

#table2中

+----------------------------------------+
| name    dpens  dpencils  dbooks        |
+----------------------------------------+
| ramesh    4        5        9          |
| prasad    5        6        7          |
| hari      7        8        9          |
+----------------------------------------+

我想要像

这样的结果
+------------------------------------------------------------------------------------------+
| name     Tpens  Tpencils Tbooks dpens dpencils dbooks                                    |
+------------------------------------------------------------------------------------------+
| suresh   1        2        5     0        0       0    no null values                    |
| ramesh   3        5        7     4        5       9    Ramesh has values in both tables  |
| ravi     2        7        9     0        0       0                                      |
| prasad   0        0        0     5        6       7                                      |
| hari     0        0        0     7        8       9                                      |
+------------------------------------------------------------------------------------------+

我写了这个查询:

select table1.name,
        table1.tpens, 
        table1.tpencils ........etc......
        table2.dbooks 
from table1 
full outer join table2 on table1.name = table2.name

它给了我一些空值,但prasadhari没有出现在结果表中。

3 个答案:

答案 0 :(得分:0)

这样的事情应该这样做。

select t1.tpens
    , t1.tpencils
    , t1.tbooks
    , isnull(t2.dpens, 0) as dpens
    , isnull(t2.dpencils, 0) as dpencils
    , isnull(t2.dbooks, 0) as dbooks
from #table1 t1
left join #table2 t2 on t1.name = t2.name

答案 1 :(得分:0)

这应该实现你的目标:

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE one(
  name NVARCHAR(40) PRIMARY KEY,
  Tpens INT
 )
CREATE TABLE two(
  name NVARCHAR(40) PRIMARY KEY,
  Dpens INT
 )
 INSERT INTO one VALUES('bob', 5),('dave', 6), ('ravi',9)
 INSERT INTO two VALUES('bill', 4),('dave', 3), ('ravi',2)

查询1

SELECT
   CASE WHEN one.name IS NULL THEN two.name ELSE one.name END
  ,ISNULL(one.Tpens,0)
  ,ISNULL(two.Dpens,0)
FROM one 
FULL OUTER JOIN two on one.name = two.name

<强> Results

|      |   |   |
|------|---|---|
| bill | 0 | 4 |
|  bob | 5 | 0 |
| dave | 6 | 3 |
| ravi | 9 | 2 |

答案 2 :(得分:0)

这可以解决您的问题:

select 
    isnull(t1.name,t2.name) as name,
    isnull(t1.Tpens, 0) as Tpens,
    isnull(t1.Tpencils, 0) as Tpencils,
    isnull(t1.Tbooks, 0) as Tbooks,
    isnull(t2.Dpens, 0) as Dpens,
    isnull(t2.Dpencils, 0) as Dpencils,
    isnull(t2.Dbooks, 0) as Dbooks
from table1 as t1
full outer join table2 as t2 
    on t1.name = t2.name

看看这个有用的SQLFiddle http://sqlfiddle.com/#!3/5bec0/7