Alias(别名)
eg(使用表名称别名):
select po.orderid,p.lastname,p.firstname from persons as p,product_orders as po where p.lastname='adams' and p.firstname='john'(使用别名)
select product_orders.orderid,persons.lastname,persons.firstname from persons,product_orders where persons.lastname='adams' and persons.firstname='john'(不使用别名)
eg(使用一个列名别名):
select lastname as family,firstname as name from persons
SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据。
引用两个表:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons, Orders WHERE Persons.Id_P = Orders.Id_P
SQL JOIN - 使用 Join
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P ORDER BY Persons.LastName
inner join 与join 是相同的。在表中存在至少一个匹配时,inner join 关键字返回行。
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
select Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON
Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON
Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
FULL JOIN 关键字 :只要其中某个表存在匹配,FULL JOIN 关键字就会返回行。
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON
Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
UNION 和 UNION ALL 操作符
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。
eg:
SELECT E_Name FROM Employees_China
UNION
SELECT E_Name FROM Employees_USA
SELECT E_Name FROM Employees_China
UNION ALL
SELECT E_Name FROM Employees_USA