The use of view in Mysql and the skill of multiple table inner join sharing _mysql

Source: Internet
Author: User
Tags joins one table

Create a View

SQL code

CREATE VIEW view_name 
as SELECT t1.xxx, t2.xxx, t3.xxx from table1 T1 
-INNER 
JOIN table2 on t2 = T1.fid ) 
INNER JOIN table3 t3 on t1.mid = T3.mid; 

3 table associations are used here, and there is a trick to INNER JOIN notation for multiple table associations

1. First write the simplest 2-table Association INNER JOIN
2. Then use () to extend all the words from the post to the end of the statement
3. Start the connection with the INNER join of the next table at the end of the statement

Remember this principle, in the Future 4-table association, 5-Table association is not a difficult thing

Delete View

Copy Code code as follows:

DROP VIEW view_name

This is supplemented by other netizens:

Multi-table joins are a very useful technique because in some cases we need to query data across multiple tables.

Syntax format:
From ((Table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER join table 4 on member. Field number = Table 4. Field no. = INNER Join Table x on member. Field number = table X. field number, just apply the format.

Precautions:
In the input letter process, must use the English Half-width punctuation mark, the word leaves the half corner space;
When you create a datasheet, if a table joins more than one table, the fields in this table must be of the number data type, and the same field in more than one table must be a primary key and an AutoNumber data type. Otherwise, it is difficult to connect successfully.
Code nesting Quick Method: For example, to connect five tables, just add a front and back bracket to the code that connects the four tables (the parentheses are appended to the from, the parentheses are appended to the end of the code), and then continue with the INNER JOIN table name x on table 1 after the parentheses. Field number = Table X. Field number Code, so you can join the data table indefinitely.

Use to connect two data tables:
from table 1 INNER JOIN table 2 on table 1. Field number = Table 2. Field number

Use to connect three data tables:
from (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER JOIN table 3 on table 1. Field number = Table 3. Field number

Use to connect four data tables:
from (table 1 INNER JOIN table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER join table 4 on member. Field number = Table 4.

uses five data tables:
from ((Table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER Join Table 4 on member. Field number = Table 4. Field number INNER JOIN table 5 on member. Field number = Table 5. Field number

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.