Simple comparison of three join query methods in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces the simple comparison of the three associated query methods in MySQL, that is, ON and USING, and the traditional FROM... WHERE ..., for more information, see the following SQL statements for the three associated queries?

SELECT * FROM film JOIN film_actor ON (film.film_id = film_actor.film_id)SELECT * FROM film JOIN film_actor USING (film_id)SELECT * FROM film, film_actor WHERE film.film_id = film_actor.film_id

The biggest difference is syntactic sugar, but there are some interesting things worth noting.

To facilitate the difference, we call the first two writing methods ANSI style and the third one Theta style.
Theta Style

The associated table names are listed in the FROM phrase, while the WHERE phrase specifies how to associate.

This method is considered to be an old method and is difficult to understand in some cases. for details, refer to the following query:

SELECT * FROM film, film_actor WHERE film.film_id = film_actor.film_id AND actor_id = 17 AND film.length > 120

The above query lists movies with a length of more than 120 minutes, including the condition that the actor number is 17. Do not care about the query results. what is the query itself? The WHERE expression contains three conditions. you need to see which condition is associated and which condition is filter or slightly charged. However, it is relatively simple, but what if there are five tables and more than 20 conditions?
ANSI style: ON

JOIN... ON can be used to separate the table JOIN conditions from the record filter conditions. The result of overwrite the preceding statement is as follows:

SELECT * FROM film JOIN film_actor ON (film.film_id = film_actor.film_id) WHERE actor_id = 17 AND film.length > 120

It looks much clearer.

Note: parentheses in the ON statement are not mandatory. I personally prefer to write them like this.

ANSI style: USING

In a special case, when the field names of two tables to be joined are the same, we can use USING to reduce the length of the SQL statement:

SELECT * FROM film JOIN film_actor USING (film_id) WHERE actor_id = 17 AND film.length > 120

Brackets are required at this time. This is a good way to write, enter less words, and query performance is also good, but pay attention to some differences.

USING and ON

The following statement is feasible:

SELECT film.title, film_id FROM film JOIN film_actor USING (film_id) WHERE actor_id = 17 AND film.length > 120;

But the following won't work:

SELECT film.title, film_id FROM film JOIN film_actor ON (film.film_id = film_actor.film_id) WHERE actor_id = 17 AND film.length > 120;ERROR 1052 (23000): Column 'film_id' in field list is ambiguous

Because the USING "knows" film_id field exists in both tables, it does not matter if no exact table is specified. the two values must be consistent.

ON is not so intelligent. you must specify the table and field name to be associated.

The above two actual results are interesting. when USING, the field appears only once in the result:

SELECT * FROM film JOIN film_actor USING (film_id) WHERE actor_id = 17 AND film.length > 120 LIMIT 1\G*************************** 1. row ***************************       film_id: 96        title: BREAKING HOME     description: A Beautiful Display of a Secret Agent And a Monkey who must Battle a Sumo Wrestler in An Abandoned Mine Shaft    release_year: 2006     language_id: 1original_language_id: NULL   rental_duration: 4     rental_rate: 2.99       length: 169  replacement_cost: 21.99       rating: PG-13  special_features: Trailers,Commentaries     last_update: 2006-02-15 05:03:42      actor_id: 17     last_update: 2006-02-15 05:05:03

When ON is used, the field appears twice:

SELECT * FROM film JOIN film_actor ON film.film_id = film_actor.film_id WHERE actor_id = 17 AND film.length > 120 LIMIT 1\G*************************** 1. row ***************************       film_id: 96        title: BREAKING HOME     description: A Beautiful Display of a Secret Agent And a Monkey who must Battle a Sumo Wrestler in An Abandoned Mine Shaft    release_year: 2006     language_id: 1original_language_id: NULL   rental_duration: 4     rental_rate: 2.99       length: 169  replacement_cost: 21.99       rating: PG-13  special_features: Trailers,Commentaries     last_update: 2006-02-15 05:03:42      actor_id: 17       film_id: 96     last_update: 2006-02-15 05:05:03

Behind scenes

MySQL processes the two in the same way. using explain extended, we can see that:

EXPLAIN EXTENDED SELECT film.title, film_id FROM film JOIN film_actor USING (film_id) WHERE actor_id = 17 AND film.length > 120\G*************************** 1. row ***************************...2 rows in set, 1 warning (0.00 sec) root@mysql-5.1.51> SHOW WARNINGS\G*************************** 1. row *************************** Level: Note  Code: 1003Message: select `sakila`.`film`.`title` AS `title`,`sakila`.`film`.`film_id` AS `film_id`     from `sakila`.`film` join `sakila`.`film_actor`     where (         (`sakila`.`film`.`film_id` = `sakila`.`film_actor`.`film_id`)         and (`sakila`.`film_actor`.`actor_id` = 17)         and (`sakila`.`film`.`length` > 120)        )

Eventually all the queries are converted into Theta.

Translator: that is to say, there is no difference between the three methods except for different writing methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.