Chapter XIV using subqueries
Subqueries
Example: Query all customer information TNT2 of items ordered
//1:检索包含物品TNT2的所有订单的编号
SELECT order_num
FROM orderitems
WHERE prod_id = 'TNT2';
//2检索具有前一步骤列出的订单编号的所有客户的id
SELECT cust_id
FROM orders
WHERE order_num IN (20005,20007);
//3检索上一步所有客户id的全部信息
SELECT cust_name,cust_contact
FROM customers
WHERE cust_id IN (10001,10004);
//将上面3步合并
SELECT cust_name,cust_contact
FROM customers
WHERE cust_id IN (SELECT cust_id
FROM orders
WHERE order_num IN (SELECT order_num
FROM orderitems
WHERE prod_id = 'TNT2'));
Using Subqueries as calculated fields
The total number of orders for each customer customers display table
SELECT cust_id,
cust_name,
cust_state,(SELECT count(*)
FROM orders
WHERE orders.cust_id = customers.cust_id) AS num_order
FROM customers
ORDER BY num_order
Joining tables
Creating links
A foreign key to a table, which contains the primary key of another table, defines the relationship between the two tables
advantages
- Supplier information is not repeated, so as not to waste time and space;
- If the supplier information changes, vendors can update only a single record in the table, data from related tables do not change;
- Since no duplicate data obviously data are consistent, which makes it easier to process the data.
SELECT vend_name,prod_name,prod_price
FROM vendors v,products p
WHERE v.vend_id = p.vend_id
ORDER BY vend_name,prod_name
Cartesian product (cartesian product) from the results table relationships not coupled condition returned by Cartesian product. Number of rows retrieved will be the number of rows in the first table multiplied by the number of rows in the second table.
SELECT vend_name,prod_name,prod_price
FROM vendors v,products p
//将显示笛卡儿积
Equivalent link
As we used so far called equivalent coupling links (equijoin), which is based on equality test between the two tables. This link is also known as internal links. In fact, for such a link may use a slightly different syntax to explicitly specify the type of link. The following SELECT statement returns exactly the same data in the previous example:
SELECT vend_name,prod_name,prod_price
FROM vendors v
INNER JOIN products p ON v.vend_id = p.vend_id
ORDER BY vend_name,prod_name
Join multiple tables
SELECT prod_name,vend_name,prod_price,quantity
FROM products p, orderitems o,vendors v
WHERE p.vend_id =v.vend_id
AND o.prod_id = p.prod_id
AND o.order_num = 20005;
Chapter XVI Create advanced link
In addition to the column names and aliases calculated fields general, SQL allows for a table name aliases . There are two main reasons for doing so:
- Shorten the SQL statement;
- Allow for multiple use of the same single table in the SELECT statement.
Using different types of links
Four kinds of links: the equivalent of association, self-join, natural links and external links.
Self-join
If you find an article (with ID DTNTR) problems, and wondered whether the production of the goods produced by other suppliers of goods there are these problems. This query requires that you first find suppliers for the production of ID DTNTR items, then find other articles that a vendor.
SELECT p1.prod_id,p1.prod_name
FROM products p1, products p2
WHERE p1.vend_id = p2.vend_id
AND p2.prod_id = 'DTNTR';
Self-coupling instead of subqueries self-join statement is used generally as an outer alternative codon usage when retrieving data from the table in the same query. Although the end result is the same, but sometimes linking process much faster than processing sub-queries. You should try two approaches to determine which is better performance.
Natural links
Whenever you join the table, there should be at least a now more than one table (to be linked column) listed. Standard coupling returns all data, even the same column more than once. Exclude natural link appears more than once, so that each column returns only once.
External links
SELECT c.cust_id,o.order_num
FROM customers c
LEFT JOIN orders o ON c.cust_id = o.cust_id