我正在尝试在SQL Server中内部连接两个表。我有一个sintax错误,我相信它与我在第一个表中用于GROUP BY的代码行相关联:
SELECT TOP (10000) MAX (PlanID) AS [PlanID]
,MAX (InfoID) AS [InfoID]
,MAX (Comp) AS [Comp]
,MAX (CompName) AS [CompName]
,MAX (GuidID) AS [GuidID]
,MAX (Object) AS [Object]
,OrderID
,CurrencyShort
FROM [dbo].[Plant] as Plimp
WHERE [CompName] NOT LIKE 'BB%'
and InfoID < 196673
and InfoID > 185679
GROUP BY (InfoID)
order by InfoID desc
INNER JOIN [dbo].[Order] as Ord
ON Plimp.InfoID = Ord.InfoID
错误讯息:
第15行,第15行,第1行,第19行 关键字&#39; INNER&#39;附近的语法不正确。
第二次尝试:
SELECT TOP (10000) MAX (PlanID) AS [PlanID]
,MAX (InfoID) AS [InfoID]
,MAX (Comp) AS [Comp]
,MAX (CompName) AS [CompName]
,MAX (GuidID) AS [GuidID]
,MAX (Object) AS [Object]
,OrderID /*field from [dbo].[Order]*/
,CurrencyShort /*field from [dbo].[Order]*/
FROM [dbo].[PlanningImplant] AS Plimp
INNER JOIN [dbo].[Order] AS Ord
ON Plimp.InfoID = Ord.InfoID
WHERE [CompName] NOT LIKE 'BB%'
AND Plimp.InfoID < 196673
AND Plimp.InfoID > 185679
GROUP BY ( Plimp.InfoID )
ORDER BY Plimp.InfoID DESC
错误:
Msg 207,Level 16,State 1,Line 15 列名称无效&#39; InfoID&#39;。 Msg 207,Level 16,State 1,Line 15 列名称无效&#39; InfoID&#39;。 Msg 207,Level 16,State 1,Line 16 列名称无效&#39; CompName&#39;。 Msg 207,Level 16,State 1,Line 17 列名称无效&#39; InfoID&#39;。 Msg 207,Level 16,State 1,Line 18 列名称无效&#39; InfoID&#39;。 Msg 207,Level 16,State 1,Line 19 列名称无效&#39; InfoID&#39;。 Msg 209,Level 16,State 1,Line 3 不明确的列名称&#39; InfoID&#39;。 Msg 207,Level 16,State 1,Line 20 列名称无效&#39; InfoID&#39;。
注意: &#34; [INFOID]&#34;列中&#34; [dbo]。[植物]&#34;最初提供重复项,我在原始表中删除了&#34; WHERE [CompName] NOT LIKE&#39; BB%&#39;&#34;。这里的问题是这个条件是在Inner Join之后,但我不能把它放在之前,或者我得到一个错误...也许我应该使用一些括号?
答案 0 :(得分:0)
INNER JOIN
子句必须在where
和group by
子句之前出现:
SELECT TOP (10000) MAX (PlanID) AS [PlanID]
,MAX (InfoID) AS [InfoID]
,MAX (Comp) AS [Comp]
,MAX (CompName) AS [CompName]
,MAX (GuidID) AS [GuidID]
,MAX (Object) AS [Object]
,OrderID
,CurrencyShort
FROM [dbo].[Plant] as Plimp
INNER JOIN [dbo].[Order] as Ord
ON Plimp.InfoID = Ord.InfoID
WHERE [CompName] NOT LIKE 'BB%'
and InfoID < 196673
and InfoID > 185679
GROUP BY (InfoID)
order by InfoID desc
答案 1 :(得分:0)
Join
将在Where
和Order by
之前到来,所有表格连接将一起出现在From
条款中
FROM [dbo].[Plant] AS Plimp
INNER JOIN [dbo].[Order] AS Ord
ON Plimp.InfoID = Ord.InfoID
WHERE [CompName] NOT LIKE 'BB%'
AND Ord.InfoID < 196673
AND Ord.InfoID > 185679
GROUP BY ( Ord.InfoID )
ORDER BY Ord.InfoID DESC