MySQL must know must know the study notes Chapter 16 Creating Advanced Connections

The reason for aliasing the table is:
1. Shorten the SQL statement.
2. Allow multiple use of the same table in a single SELECT statement (available for self-joining).

SELECT title, name
FROM film f, language l
WHERE f.language_id = l.language_id;

The SQL return of fetching movies and languages ​​in the previous chapter is the same. Table aliases can also be used in SELECT lists, ORDER BY clauses, and other parts of the statement.

Table aliases are only used in query execution and will not be returned to the client like column aliases.

Insert picture description here
Find the movie name and language in the film table that are the same as the language used by a movie:

SELECT f1.title 
FROM film f1, film f2 
WHERE f1.language_id = f2.language_id AND f2.film_id = 1;

The above query SELECT clause must use qualified column names, because it is impossible to distinguish which table the column names come from.

The above query can also be completed using subqueries:

SELECT title
FROM film
WHERE film.language_id = (SELECT language_id 
                          FROM film
                          WHERE film_id = 1
                         );

Joins are usually more efficient than subqueries.

When joining the tables, there should be at least one listed in more than one table (columns being joined). The standard connection returns all data, the same column will appear multiple times, and the natural connection excludes multiple occurrences, so that each column is returned only once. But the system does not complete this work, you need to do it yourself, just do not use the same column in the SELECT clause.

External links can return lines that are not associated. For example, when listing the number of movies in a language, some languages ​​do not use it, and the internal link will not return the number of movies in this language:

SELECT COUNT(f.title), l.name
FROM film f INNER JOIN language l
ON f.language_id = l.language_id
GROUP BY l.name;

Run it: and the
Insert picture description here
outer join can select rows that are not in another table:

SELECT COUNT(f.title), l.name
FROM film f RIGHT OUTER JOIN language l
ON f.language_id = l.language_id
GROUP BY l.name;

Run it: the
Insert picture description here
left and right links depend on which table you want to keep all rows. As in the above example, you want to keep all the language rows. Even if there is no movie in the language, use the right link. If the language table is on the left and the movie table is on the right, you must To achieve the same purpose, use the left link.

The COUNT function in the above example selects the movie name column instead of all the columns *. This is because due to the use of outer joins, it will always return one row. If all columns are used, the COUNT function will return 1 when there are no movies in the language. , And when there is no movie, the movie name is NULL, so using the COUNT column name can ignore such rows, and the COUNT column indicates that only non-NULL rows will be counted.

MySQL does not support *=and =*operators, they are very popular in other DBMS.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111227505