我该如何改变观点?

时间:2014-07-30 12:32:17

标签: sql-server

如何更改看起来如下的视图:

SELECT ...    
FROM     dbo.T_CounterAgent RIGHT OUTER JOIN
         dbo.T_City RIGHT OUTER JOIN
         dbo.T_CounterAgent AS T_CounterAgent_1 RIGHT OUTER JOIN
         dbo.T_Users ON T_CounterAgent_1.ID = dbo.T_Users.CounterAgentID LEFT OUTER JOIN
         dbo.T_Cashdesk INNER JOIN
         dbo.T_ReservAmount ON dbo.T_Cashdesk.ReservAmountID = dbo.T_ReservAmount.ID ON dbo.T_Users.CashdeskID = dbo.T_Cashdesk.ID ON 
         dbo.T_City.ID = dbo.T_Users.CityID ON dbo.T_CounterAgent.ID = dbo.T_Cashdesk.CounterAgentID LEFT OUTER JOIN
         dbo.T_UserRightGroup INNER JOIN
         dbo.T_UserRight ON dbo.T_UserRightGroup.ID = dbo.T_UserRight.GroupID ON dbo.T_Users.UserRightID = dbo.T_UserRight.ID

我需要添加一个应该通过以下方式连接到T_Users表的表T_UsersPhones:

T_Users.ID - > T_UsersPhones.UserID

2 个答案:

答案 0 :(得分:2)

SELECT ...    
FROM     dbo.T_CounterAgent RIGHT OUTER JOIN
         dbo.T_City RIGHT OUTER JOIN
         dbo.T_CounterAgent AS T_CounterAgent_1 RIGHT OUTER JOIN
         dbo.T_Users ON T_CounterAgent_1.ID = dbo.T_Users.CounterAgentID LEFT OUTER JOIN
         dbo.T_Cashdesk INNER JOIN
         dbo.T_ReservAmount ON dbo.T_Cashdesk.ReservAmountID = dbo.T_ReservAmount.ID ON dbo.T_Users.CashdeskID = dbo.T_Cashdesk.ID ON 
         dbo.T_City.ID = dbo.T_Users.CityID ON dbo.T_CounterAgent.ID = dbo.T_Cashdesk.CounterAgentID LEFT OUTER JOIN
         dbo.T_UserRightGroup INNER JOIN
         dbo.T_UserRight ON dbo.T_UserRightGroup.ID = dbo.T_UserRight.GroupID ON dbo.T_Users.UserRightID = dbo.T_UserRight.ID LEFT JOIN
         dbo.T_UsersPhones ON dbo.T_Users.ID = dbo.T_UsersPhones.UserID

你应该真正开始使用alliases,因为它有助于提高可读性。

答案 1 :(得分:0)

来自top google result

ALTER VIEW HumanResources.EmployeeHireDate
AS
SELECT p.FirstName, p.LastName, e.HireDate
FROM HumanResources.Employee AS e JOIN Person.Person AS  p
ON e.BusinessEntityID = p.BusinessEntityID
WHERE HireDate < CONVERT(DATETIME,'20020101',101) ;
GO