SQL join语法,什么时候使用“AS”?

时间:2022-08-28 09:06:45

I am working on an SQL statement and just wanted to clarify my understanding of join syntax in MS SQL.

我正在研究一个SQL语句,只是想澄清我对MS SQL中的join语法的理解。

Say I have the SQL...

假设我有SQL…

select t.year from HOUSE AS h 
LEFT OUTER JOIN SUBJECT K 
    ON H.Name = K.Name 
INNER JOIN TESTCASE AS T 
    ON k.year IS NOT NULL

Sorry for the confusing example but basically - why am I able to use LEFT OUTER JOIN SUBJECT K without the keyword AS whereas with an INNER JOIN, I need to use a keyword of AS for INNER JOIN TESTCASE AS T?

不好意思,这个例子有点混乱,但基本上,为什么我不用关键字就能使用左外连接主题K而对于内连接,我需要使用关键字AS作为内连接TESTCASE的T?

4 个解决方案

#1


4  

'AS' is not required in either of these cases, but I prefer it personally from the point of view of readability, as it conveys exactly what you were meaning to do.

在这两种情况下,“AS”都不是必需的,但从可读性的角度来看,我更喜欢它,因为它准确地表达了你的意思。

#2


1  

As far as I know the AS is only for easy coding. You can create a smaller or more clear name for the table. So House as H and further in the query you can use H.Name instead of typing House.Name

就我所知,a只是用于简单的编码。您可以为该表创建一个更小或更清晰的名称。所以House作为H,在查询中你可以使用H。不要键入House.Name

#3


0  

I believe 'AS' used to be a required keyword, but is no longer needed, only for readability. I find 'As' is often used when creating a name for a data item in the SELECT, but not when giving a name to a table in a JOIN. For example:

我认为“AS”曾经是一个必需的关键字,但现在不再需要了,只是为了可读性。我发现在为SELECT中的数据项创建名称时经常使用“As”,但在为JOIN中的表提供名称时则不使用“As”。例如:

SELECT t.ID as TestID FROM [House] h JOIN [TestCase] t ON t.ID=h.TestID

I think this is probably because a set of data items displayed with no 'As' could become confusing as to how many items there are, and how many are just names applied to them.

我认为这可能是因为一组显示没有“As”的数据项可能会让人感到困惑,不知道有多少项,有多少项仅仅是应用于它们的名称。

#4


0  

interesting though where the AS keyword is useful... lets say two tables have name and you get this back from a query in code then it is cool to change the column name to something unique.

有趣的是,AS关键字是有用的……假设有两个表有名称,然后从代码中的查询中得到这个,然后将列名更改为唯一的东西是很酷的。

#1


4  

'AS' is not required in either of these cases, but I prefer it personally from the point of view of readability, as it conveys exactly what you were meaning to do.

在这两种情况下,“AS”都不是必需的,但从可读性的角度来看,我更喜欢它,因为它准确地表达了你的意思。

#2


1  

As far as I know the AS is only for easy coding. You can create a smaller or more clear name for the table. So House as H and further in the query you can use H.Name instead of typing House.Name

就我所知,a只是用于简单的编码。您可以为该表创建一个更小或更清晰的名称。所以House作为H,在查询中你可以使用H。不要键入House.Name

#3


0  

I believe 'AS' used to be a required keyword, but is no longer needed, only for readability. I find 'As' is often used when creating a name for a data item in the SELECT, but not when giving a name to a table in a JOIN. For example:

我认为“AS”曾经是一个必需的关键字,但现在不再需要了,只是为了可读性。我发现在为SELECT中的数据项创建名称时经常使用“As”,但在为JOIN中的表提供名称时则不使用“As”。例如:

SELECT t.ID as TestID FROM [House] h JOIN [TestCase] t ON t.ID=h.TestID

I think this is probably because a set of data items displayed with no 'As' could become confusing as to how many items there are, and how many are just names applied to them.

我认为这可能是因为一组显示没有“As”的数据项可能会让人感到困惑,不知道有多少项,有多少项仅仅是应用于它们的名称。

#4


0  

interesting though where the AS keyword is useful... lets say two tables have name and you get this back from a query in code then it is cool to change the column name to something unique.

有趣的是,AS关键字是有用的……假设有两个表有名称,然后从代码中的查询中得到这个,然后将列名更改为唯一的东西是很酷的。