SELECT P.ProductName,
P.ProductGroup,
P.ProductRetailPrice
FROM Products AS P
例子2
SELECT P.ProductName,
P.ProductRetailPrice,
O.Quantity
FROM Products AS P
LEFT OUTER JOIN Orders AS O ON O.ProductID = P.ProductID
WHERE O.OrderID = 123456
例子3
使用 AS 关键字是一个很好的实践,并且非常推荐,但是不使用 AS 关键字也可以执行相同的查询(我经常这样做)。
SELECT P.ProductName,
P.ProductRetailPrice,
O.Quantity
FROM Products P
LEFT OUTER JOIN Orders O ON O.ProductID = P.ProductID
WHERE O.OrderID = 123456
正如您所看到的,我在上一个示例中省略了 AS 关键字。也可以用作化名。
例子4
SELECT P.ProductName AS "Product",
P.ProductRetailPrice AS "Retail Price",
O.Quantity AS "Quantity Ordered"
FROM Products P
LEFT OUTER JOIN Orders O ON O.ProductID = P.ProductID
WHERE O.OrderID = 123456
例4的输出
Product Retail Price Quantity Ordered
Blue Raspberry Gum $10 pk/$50 Case 2 Cases
Twizzler $5 pk/$25 Case 10 Cases
<table reference> ::=
<table name> [ [ AS ] <correlation name>
[ <left paren> <derived column list> <right paren> ] ]
| <derived table> [ AS ] <correlation name>
[ <left paren> <derived column list> <right paren> ]
| <joined table>
<derived table> ::= <table subquery>
<derived column list> ::= <column name list>
<column name list> ::=
<column name> [ { <comma> <column name> }... ]
Syntax Rules
1) A <correlation name> immediately contained in a <table refer-
ence> TR is exposed by TR. A <table name> immediately contained
in a <table reference> TR is exposed by TR if and only if TR
does not specify a <correlation name>.
SELECT e.EmployeeID, s.CompanyName, o.ShipName
FROM Employees AS e INNER JOIN
Orders AS o ON e.EmployeeID = o.EmployeeID INNER JOIN
Shippers AS s ON o.ShipVia = s.ShipperID
WHERE (s.CompanyName = 'Federal Shipping')
然而,删除 AS 并不会产生任何下列差异:
SELECT e.EmployeeID, s.CompanyName, o.ShipName
FROM Employees e INNER JOIN
Orders o ON e.EmployeeID = o.EmployeeID INNER JOIN
Shippers s ON o.ShipVia = s.ShipperID
WHERE (s.CompanyName = 'Federal Shipping')