Alias(別名)spa
eg(使用表名稱別名): table
select po.orderid,p.lastname,p.firstname from persons as p,product_orders as po where p.lastname='adams' and p.firstname='john'(使用別名)ast
select product_orders.orderid,persons.lastname,persons.firstname from persons,product_orders where persons.lastname='adams' and persons.firstname='john'(不使用別名)
select
eg(使用一個列名別名):
引用
select lastname as family,firstname as name from persons
im
SQL join 用於根據兩個或多個表中的列之間的關係,從這些表中查詢數據。數據
引用兩個表:
查詢
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons, Orders WHERE Persons.Id_P = Orders.Id_P margin
SQL JOIN - 使用 Joinimg
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