内部加入2个单独的查询

时间:2014-05-08 22:21:44

标签: sql-server tsql

我喜欢加入数据集。

不确定以下行为无效:

    (Select  LocID,  ParamID,  alertNumExceed, upperAlarm,  lowerAlarm,  alertOn,  EntryUserID,  ParamOrder 
     from data_LocParams )  dl 
     inner join 
    (SELECT LocId 
    from map_Sites ms
    inner join map_WaterSystems mw on mw.SiteId = ms.SiteId
    inner join map_Locations ml on mw.SysID = ml.SysID
    where ms.SiteId = 344 )  mq
    on dl.LocID = mq.LocId   

我收到以下错误:

Msg 102,Level 15,State 1,Line 3 ' dl'附近的语法不正确。 Msg 102,Level 15,State 1,Line 9 &mq'。

附近的语法不正确

2 个答案:

答案 0 :(得分:1)

你还没有选择任何东西

SELECT dl.LocID,  ParamID,  alertNumExceed, upperAlarm,  lowerAlarm,  alertOn,  EntryUserID,  ParamOrder  
FROM
    (Select  LocID,  ParamID,  alertNumExceed, upperAlarm,  lowerAlarm,  alertOn,  EntryUserID,  ParamOrder 
     From data_LocParams )  dl 
     inner join 
    (SELECT LocId 
    from map_Sites ms
    inner join map_WaterSystems mw on mw.SiteId = ms.SiteId
    inner join map_Locations ml on mw.SysID = ml.SysID
    where ms.SiteId = 344 )  mq
    on dl.LocID = mq.LocId

答案 1 :(得分:0)

假设map_Locations具有名为LocId的列,查询可以简化为

Select  dl.LocID,  dl.ParamID,  dl.alertNumExceed, dl.upperAlarm,  dl.lowerAlarm,  dl.alertOn,  dl.EntryUserID,  dl.ParamOrder 
from data_LocParams dl 
inner join map_Locations ml on dl.LocId = ml.DocId
inner join map_WaterSystems mw on mw.SysID = ml.SysID
inner join map_Sites ms on mw.SiteId = ms.SiteId
WHERE ms.SiteId = 344

Select  dl.LocID,  dl.ParamID,  dl.alertNumExceed, dl.upperAlarm,  dl.lowerAlarm,  dl.alertOn,  dl.EntryUserID,  dl.ParamOrder 
from map_Sites ms
inner join map_WaterSystems mw on mw.SiteId = ms.SiteId
inner join map_Locations ml on mw.SysID = ml.SysID
inner join from data_LocParams on dl.LocID = mq.LocId   
on dl.LocID = mq.LocId