我有3张桌子:Entry
,User
,Comment
。我通过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.
我无法解决这个问题,需要帮助。感谢。
答案 0 :(得分:2)
查询视图时,视图定义中使用的别名不可用。所以这个:
SELECT * FROM Entry_View WHERE E.Department = 'Administration'
必须成为这个:
SELECT * FROM Entry_View E WHERE E.Department = 'Administration'
或者这个:
SELECT * FROM Entry_View WHERE Department = 'Administration'
答案 1 :(得分:1)
您选择E.Department AS Department ....所以
SELECT * FROM Entry_View WHERE Department = 'Administration'
答案 2 :(得分:1)
您可以尝试:SELECT * FROM Entry_View WHERE Department = 'Administration'