Sql server 内连接时的情况

Sql server 内连接时的情况,sql-server,case,inner-join,Sql Server,Case,Inner Join,我有以下4个表:主表、仓库、客户和公司 这些表的架构: create table MainTable(ID int, Warehouse_id int, Customer_ID int) create table Warehouse (Warehouse_id int, company_id int) create table Customer (Customer_ID int, Company_ID int) create table company (Company_id int, Coun

我有以下4个表:主表、仓库、客户和公司

这些表的架构:

create table MainTable(ID int, Warehouse_id int, Customer_ID int)
create table Warehouse (Warehouse_id int, company_id int)
create table Customer (Customer_ID int, Company_ID int)
create table company (Company_id int, Country_ID int, Zone_ID int)
目标是获取(MainTable的)相应ID的国家ID和地区ID

我们有两种不同的情况:
如果MainTable.Warehouse\u ID
不为空,我们应该对Warehouse表(在field Warehouse\u ID上)进行内部联接,然后对Company表(在field Company\u ID上)进行联接,
else
(如果MainTable.Warehouse\u ID为空),我们应该先在Customer表(Customer\u ID字段)上执行内部联接,然后在Company表(Company\u ID字段)上执行内部联接

以下查询在单词“case”附近生成错误:

select CO.Country_ID, CO.Zone_ID
from    MainTable MT 
inner join (case 
    when MT.Warehouse_ID is not null 
then
         Warehouse W on MT.Warehouse_ID=W.Warehouse_ID 
        inner join Company CO on W.Company_ID=CO.Company_ID
else     
         Customer Cu on MT.Customer_ID=Cu.Customer_ID
        inner join Company C on Cu.Company_ID=CO.Company_ID
    end)
我是不是因为错过了一个小的语法错误而做得对?如果没有,还有别的办法吗


谢谢你不能那样做对于这种条件联接,您希望输出是什么

你试过使用外部连接吗

select ISNULL(C1.Country_ID,C2.Country_ID) as Country_ID,ISNULL(C1.Zone_ID,C2.Zone_ID) as Zone_ID
from    MainTable MT 
left outer join  Warehouse W on MT.Warehouse_ID=W.Warehouse_ID 
left outer join Company1 C1 on W.Company_ID=C1.Company_ID
left outer join Customer Cu on MT.Customer_ID=Cu.Customer_ID
left outer join Company2 C2 on Cu.Company_ID=C2.Company_ID
您可以使用:


尝试以下方法:

IF EXISTS(SELECT 1
          FROM   MainTable mt
          WHERE  mt.Warehouse_ID IS NOT NULL)
  BEGIN
      SELECT CO.Country_ID,
             CO.Zone_ID
      FROM   MainTable MT
             INNER JOIN Warehouse W
                     ON MT.Warehouse_ID = W.Warehouse_ID
             INNER JOIN Company CO
                     ON W.Company_ID = CO.Company_ID
  END
ELSE
  BEGIN
      SELECT CO.Country_ID,
             CO.Zone_ID
      FROM   MainTable MT
             INNER JOIN Customer Cu
                     ON MT.Customer_ID = Cu.Customer_ID
             INNER JOIN Company C
                     ON Cu.Company_ID = CO.Company_ID
  END 

一种方法是通过动态查询!但是您需要使用一个存储过程,因为条件是特定于每一行的,所以Dynamic SQL不会有任何帮助。对于减号,请?谢谢大家的帮助。托马斯,用左外连接,它工作:)
IF EXISTS(SELECT 1
          FROM   MainTable mt
          WHERE  mt.Warehouse_ID IS NOT NULL)
  BEGIN
      SELECT CO.Country_ID,
             CO.Zone_ID
      FROM   MainTable MT
             INNER JOIN Warehouse W
                     ON MT.Warehouse_ID = W.Warehouse_ID
             INNER JOIN Company CO
                     ON W.Company_ID = CO.Company_ID
  END
ELSE
  BEGIN
      SELECT CO.Country_ID,
             CO.Zone_ID
      FROM   MainTable MT
             INNER JOIN Customer Cu
                     ON MT.Customer_ID = Cu.Customer_ID
             INNER JOIN Company C
                     ON Cu.Company_ID = CO.Company_ID
  END