Blog Reader RSS LoveCherry 技术无极限 GEO MVP MS Project开源技术

What is a full join? How is it different from other joins?

A "FULL JOIN" is an outer join that takes *all* data from both tables, matched where it can, as opposed to a LEFT or RIGHT join that takes all the data from one table, *and* any matching records from the other table,

Examples:

CUSTOMER table contains details of customers
DISCOUNTDEAL table contains details of different discount deals

You want to see all customers, and list the discount deal they have if they have one:

SELECT *
FROM CUSTOMER
LEFT JOIN DISCOUNTDEAL ON CUSTOMER.DEALID=DISCOUNTDEAL.D EALID

You want to see all deals, and list the customers that have that deal if there are any:

SELECT *
FROM CUSTOMER
RIGHT JOIN DISCOUNTDEAL ON CUSTOMER.DEALID=DISCOUNTDEAL.D EALID

(you could also do a LEFT JOIN from DISCOUNTDEAL to CUSTOMER to achieve the same thing, of course)

You want to see all deals (even it no customers are using them), and all customers (even if they don't have a deal)

SELECT *
FROM CUSTOMER
FULL OUTER JOIN DISCOUNTDEAL ON CUSTOMER.DEALID=DISCOUNTDEAL.D EALID
posted @ 2008-05-01 11:31  大宋提刑官  阅读(189)  评论(0编辑  收藏  举报