【问题标题】:Using SQL VIEW with WHERE Clause (ERROR) [duplicate]将 SQL VIEW 与 WHERE 子句一起使用(错误)[重复]
【发布时间】:2013-12-24 18:58:29
【问题描述】:

我有 3 张桌子:EntryUserComment。我通过 sql 视图显示每个条目的评论计数和用户(发件人)全名。但是当我使用where 子句时,它会报错。

我的 SQL 视图是这样的:

CREATE VIEW [dbo].Entry_View
    AS SELECT 
            E.Id AS [Id], 
            Convert(varchar(10), E.Date, 104) AS [Date], 
            E.Subject AS [Subject], 
            E.Content AS [Content], 
            E.Faculty AS [Faculty], 
            E.Category AS [Category],
            (U.Firstname + ' ' + U.Lastname) AS [User],
            E.Department AS [Department],
            E.ViewCount AS [View],
            E.SupportCount AS [Support],
            Count(C.Entry_Id) AS [Comment] 
        FROM (Entry E INNER JOIN User U ON U.Id = E.User_Id)
        LEFT JOIN Comment C on C.Entry_Id=E.Id 
        GROUP BY
            E.Id, E.Date, E.Subject, E.Content, 
            E.Faculty, E.Category, (U.Firstname + ' ' + U.Lastname), 
            E.Department, E.ViewCont, E.SupportCount

当我打电话时:SELECT * FROM Entry_View WHERE E.Department = 'Administration'

我收到一个错误: The multi-part identifier "E.Department" could not be bound.

我无法解决这个问题,需要帮助。谢谢。

【问题讨论】:

  • 别名 E 未在您的 SELECT 查询中定义。
  • 这不是该线程的副本。该线程在问题定义中不包含视图。
  • @DanBracuk 该观点与这个问题完全无关。
  • 我认为它不是重复的。有很多 The multi-part identifier could not be bound 问题,但不是我的问题的确切解决方案。
  • 只是一个疯狂的猜测,但问题可能是“无法绑定多部分标识符“E.Department”。”。

标签: mysql sql


【解决方案1】:

视图定义中使用的别名在查询视图时不可用。所以这个:

SELECT * FROM Entry_View WHERE E.Department = 'Administration' 

必须变成这样:

SELECT * FROM Entry_View E WHERE E.Department = 'Administration' 

或者这个:

SELECT * FROM Entry_View WHERE Department = 'Administration' 

【讨论】:

    【解决方案2】:

    你选择 E.Department AS Department....所以

    SELECT * FROM Entry_View WHERE Department = 'Administration'
    

    【讨论】:

      【解决方案3】:

      你可以试试:SELECT * FROM Entry_View WHERE Department = 'Administration'

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        相关资源
        最近更新 更多