使用内部联接选择语句

时间:2017-11-05 22:57:44

标签: sql tsql inner-join

我对SQL很新。我在2个表之间创建了一个内连接,并进一步创建了一些where子句来提取适当的数据。据我了解,我使用内连接来连接2个表。我现在要做的是将我的结果选择查询连接到另一个表。我该怎么做?

SELECT  
t.[Type]



from [MITS].[dbo].[monster] t 

inner join (

SELECT [MITS].[dbo].[BROKERTABLE].[BrokerID]
      ,[MITS].[dbo].[CustomerRates].[MPAN_ID]
      ,[MITS].[dbo].[BROKERTABLE].[Commission_Rate]
      ,[MITS].[dbo].[BROKERTABLE].[Rate_From]
      ,[MITS].[dbo].[BROKERTABLE].[Rate_To]
      ,[MITS].[dbo].[CustomerRates].[From_Date]
      ,[MITS].[dbo].[CustomerRates].[To_Date]


  from [MITS].[dbo].[CustomerRates]

   Inner Join [MITS].[dbo].[BROKERTABLE]
  on [MITS].[dbo].[BROKERTABLE].[MPAN_ID] =
  [MITS].[dbo].[CustomerRates].[MPAN_ID]

  where

  [MITS].[dbo].[CustomerRates].[To_Date] <=
  [MITS].[dbo].[BROKERTABLE].[Rate_To]

   and
   convert(datetime,'01/11/2015',103)
   between convert(datetime,[MITS].[dbo].[CustomerRates].[From_Date],103) 
   and convert(datetime,[MITS].[dbo].[CustomerRates].[To_Date],103)

   ) d on t.MITID  = d.MPAT_ID

1 个答案:

答案 0 :(得分:1)

将额外的表添加到现有查询中:guess 1

select *
from atable a
inner join btable b on a.somecol = b.somecol
inner join extra_table t on a.somecol = t.somecol and b.somecol = t.somecol2

将现有查询添加到表格,方法1

select *
from extra_table t
inner join (
            your existing query here
           ) d on t.somecol = d.somecol

将现有查询添加到表格,方法2

select *
from (
            your existing query here
      ) d 
inner join extra_table t on d.somecol = t.somecol