sql join无法找到多部分标识符

时间:2013-06-25 22:30:09

标签: tsql join

这是我的查询

SELECT cont.FILTER_VALUE as filter,
    o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
     o.MODIFIED as Modified,  u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName--,p.[PAGE_NAME] as PageName 
    FROM 
     [OBJECT] AS o
     LEFT OUTER JOIN [CONTAINER] as cont 
     on cont.[OBJECT_ID] = o.[OBJECT_ID]

     LEFT JOIN [OBJECT_VALUES] AS ov ON
     ov.[OBJECT_ID] = o.[OBJECT_ID]
     LEFT JOIN [PAGE] AS p ON o.[PAGE_ID] = p.[PAGE_ID] 
     INNER JOIN [USERS] as u on u.[USER_ID] = o.LAST_MODIFIED_BY INNER JOIN [PAGE_TEMPLATE] as t 
     on o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID] INNER JOIN [site] as s on t.SITE_ID = s.SITE_ID 
     WHERE
     s.SITE_ID = '34' --AND сont.[FILTER_VALUE] is null--like '%fff%'

它很好用,直到我删除评论。

这里有很多连接,但它仍然有意义。我inner join主表与其他几个,left join可选,所以,我有一个列,包含cont.FILTER_VALUE as filter,在某些记录中为空,我可以得到它,但是我不能通过这个字段过滤。

我得到The multi-part identifier "сont.FILTER_VALUE" could not be bound.

我查看了类似的主题,但没有找到有用的信息。我不使用任何旧的SQL方言:我使用INNER/LEFT个连接,尝试group byorder by,尝试重新排序连接 - 没有任何帮助。我想我只是不明白关于联接的重要事情,请你告诉我。

感谢名单。

3 个答案:

答案 0 :(得分:0)

如果你这样写的话:
s.SITE_ID = '34' AND сont.[FILTER_VALUE] is null like '%fff%'
它的语法错误。你错过了LIKE函数的一个列(查询在哪个列中寻找模式?)。如果你没有这样写,请发表你写的内容

答案 1 :(得分:0)

我不知道你为什么会收到你的错误,但试试这个以防万一,我们等待更好的答案:

SELECT FILTER_VALUE as filter
   , o.[OBJECT_ID] as Id
   , o.[OBJECT_NAME] as Name
   , o.DESCRIPTION as Description
   , o.CREATED as Created
   , o.MODIFIED as Modified
   , u.[LOGIN] as LastModifiedByLogin
   , o.[OBJECT_NAME] as ObjectName
   , t.[TEMPLATE_NAME] as TemplateName
FROM [OBJECT] AS o
INNER JOIN [USERS] as u 
  ON u.[USER_ID] = o.LAST_MODIFIED_BY 
INNER JOIN [PAGE_TEMPLATE] as t 
  ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID] 
INNER JOIN [site] as s 
  ON t.SITE_ID = s.SITE_ID 
LEFT JOIN [CONTAINER] as cont 
  ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov 
  ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p 
  ON o.[PAGE_ID] = p.[PAGE_ID] 
WHERE s.SITE_ID = '34' AND FILTER_VALUE IS NULL

答案 2 :(得分:0)

嗯,我解决了这个问题,使用CTE,我仍然想知道,为什么没有cte我会遇到问题。

这种方式很好用

with query_CTE
AS
(
SELECT 
ROW_NUMBER() OVER (ORDER BY o.[OBJECT_ID] asc) as rowNum,
o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
 o.MODIFIED as Modified,  u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName,p.[PAGE_NAME] as PageName,

 s.SITE_ID, t.PAGE_TEMPLATE_ID, p.PAGE_ID, ov.VARIABLE_NAME, ov.VARIABLE_VALUE, cont.FILTER_VALUE, cont.DYNAMIC_CONTENT_VARIABLE, cont.SELECT_START,
 cont.SELECT_TOTAL  
FROM [OBJECT] AS o
INNER JOIN [USERS] as u 
  ON u.[USER_ID] = o.LAST_MODIFIED_BY 
INNER JOIN [PAGE_TEMPLATE] as t 
  ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID] 
INNER JOIN [site] as s 
  ON t.SITE_ID = s.SITE_ID 
LEFT JOIN [CONTAINER] as cont 
  ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov 
  ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p 
  ON o.[PAGE_ID] = p.[PAGE_ID] 

选择rowNum, ID,名称,[描述],创建,修改,LastModifiedByLogin,ObjectName,TemplateName,PageName 来自query_CTE