Home  >  Article  >  Database  >  mysql advanced join-self-join usage examples

mysql advanced join-self-join usage examples

巴扎黑
巴扎黑Original
2017-05-11 10:51:561301browse

Self-Join

As mentioned before, one of the main reasons for using table aliases is to reference the same table more than once in a single SELECT statement. Here's an example.

Suppose you find a problem with an item (whose ID is DTNTR ) and want to know if other items produced by the supplier that produces this item also have these problems. This query requires first finding the supplier

that produces the item with ID DTNTR, and then finding other items produced by this supplier.

Here is one way to solve this problem:

Input:

select prod_id,prod_name from products where vend_id = (select vend_id from products where prod_id = 'DTNTR');

Output:

mysql advanced join-self-join usage examples

Analysis: This is the first solution, which uses subqueries. The inner SELECT statement does a simple search and returns the vend_id of the item supplier whose production ID is DTNTR. This ID is used in the WHERE clause of the outer query to retrieve all items produced by this supplier.

Now look at the same query using joins:

Input:

select p1.prod_id,p1.prod_name from products as p1,products as p2 where p1.vend_id = p2.vend_id and p2.prod_id = 'DTNTR';

Output:

Analysis: This The two tables required in the query are actually the same table, so the products table appears twice in the FROM clause. Although this is perfectly legal, the reference to products is ambiguous because MySQL does not know which instance in the products table you are referring to.

mysql advanced join-self-join usage examplesTo solve this problem, table aliases are used. The first occurrence of products is the alias p1 , and the second occurrence is the alias p2 . These aliases can now be used as table names. For example, a SELECT statement uses the p1 prefix to explicitly give the full name of the required column. If not, MySQL will return an error because there are two columns named prod_id and prod_name respectively. MySQL doesn't know which column is wanted (even though they are actually the same column). WHERE (by matching vend_id in p1 and vend_id in p2) first joins the two tables, then filters the data by prod_id in the second table, returning the required data.

Use self-joins instead of subqueries. Self-joins are often used as external statements to replace subquery statements used when retrieving data from the same table. Although the end result is the same, sometimes processing joins is much faster than processing subqueries. You should try both methods to determine which performs better.

The above is the detailed content of mysql advanced join-self-join usage examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn