首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CASE语句的内部联接在SQL SERVER中不起作用。

使用CASE语句的内部联接在SQL SERVER中不起作用。
EN

Stack Overflow用户
提问于 2016-02-09 06:06:27
回答 4查看 2.4K关注 0票数 3

我有两个tables的帐号如下,

代码语言:javascript
复制
table 1:

account  

50000
50006
50015
50105
50150
50155
50165

table 2:

Account

50000
50010
50140
50105
50150
50155
50165

我需要加入这两个tables。如果table-1的任何帐户代码与table-2不匹配,那么我已经隐式地将表-1帐户代码更改为表-2帐户代码。

我做了如下的事情,

代码语言:javascript
复制
 SELECT T1.Account, T2.Account
 FROM table1 t1 
 INNER JOIN table2 t2
 on (t2.account =  CASE t1 .account 
                   WHEN 50015 THEN 50010
                   WHEN 50006 THEN 50140
                   ELSE  t1 .account 
                    END )

但我只有匹配的代码作为输出,

代码语言:javascript
复制
account Account
50000   50000
50105   50105
50150   50150
50155   50155
50165   50165

我没有得到不匹配的帐号,即(50006 and 50015)。有人能帮我找出这里出了什么问题吗?

我的预期产出是

代码语言:javascript
复制
account Account
50000   50000
50006   50140
50015   50010
50105   50105
50150   50150
50155   50155
50165   50165

谢谢你的帮助

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-02-09 06:19:26

使用CASE然后DISTINCT数据,这将为您提供通用的解决方案

代码语言:javascript
复制
-- table1
declare  @table1 table
(account  bigint)

insert into @table1 values (50000)
insert into @table1 values (50006)
insert into @table1 values (50015)
insert into @table1 values (50105)
insert into @table1 values (50150)
insert into @table1 values (50155)
insert into @table1 values (50165)

-- table2
declare  @table2 table
(account  bigint)

insert into @table2 values (50000)
insert into @table2 values (50010)
insert into @table2 values (50140)
insert into @table2 values (50105)
insert into @table2 values (50150)
insert into @table2 values (50155)
insert into @table2 values (50165)


-- QUERY
select distinct t1.account as Account1, 
Account2 = case 
    when  t1.account = t2.account then t2.account else  t1.account
    end
from @table1 t1, @table2 t2

结果

代码语言:javascript
复制
Account1    Account2
50000       50000
50006       50006
50015       50015
50105       50105
50150       50150
50155       50155
50165       50165

在评论之后,编辑--这是我们要求的一部分。我需要更新与50140中的50006帐号相对应的金额等等.

代码语言:javascript
复制
select distinct t1.account as Account1, 
Account2 = case 
    when  t1.account = 50006 then 50140
    when  t1.account = 50015 then 50010 
    else  t1.account end
from @table1 t1 , @table2 t2

结果

代码语言:javascript
复制
Account1    Account2
50000       50000
50006       50140
50015       50010
50105       50105
50150       50150
50155       50155
50165       50165
票数 2
EN

Stack Overflow用户

发布于 2016-02-09 06:12:46

试试这个

SQL Fiddle

代码语言:javascript
复制
SELECT T1.Account, T2.Account
 FROM table1 t1 
 INNER JOIN table2 t2
 on (t1.account =  CASE t1 .account 
                   WHEN 50015 THEN t2 .account+5
                   WHEN 50006 THEN t2 .account-134
                   ELSE  t2 .account 
                    END )
票数 2
EN

Stack Overflow用户

发布于 2016-02-09 06:20:06

你可以试试这个

代码语言:javascript
复制
   SELECT T1.Account, T2.Account
   FROM table1 t1 
   INNER JOIN table2 t2
   on (t2.account =  CASE 
               WHEN t1.account = 50015 THEN 50010
               WHEN t1.account=50006 THEN 50140
               ELSE  t1.account 
                END )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35285013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档