DQL multi-table query

DQL multi-table query

First, the multi-table queries
for query data across multiple tables

1. The cross-connect
Cartesian product: Each row in Table A Table B match each row in
the basic structure:
SELECT [Database Name 1] Table 1 names, attribute names 1, ......,
[database name. .] property name table
from [database name] table.;

2. Screening of cross connection conditions

SELECT [Database Name 1] Table 1 names, attribute names 1, ......,
[database name.] property name table.
[name of the database.] from table
where the conditional expression;

foreign key:

3, the connection
using comparison, data matching criteria are listed in Table matching properties

En keywords: inner join

SELECT [Database Name 1] Table 1 names, attribute names 1, ......,
[database name.] table. attribute name
from [database name.] table 1
Inner the Join [2. Database Name] Table name 2
ON conditional expression;

The difference between the cross-connection is connected to:
the ratio of the cross-connect connection more space, and more efficient

4. external connection
connector is then connected to the inner filter is divided into left and right outer joins and outer link full outer join

1. The left outer connector
is left as a reference table, the table query field display records all records left, the right and left values are shown in Table appearances match did not match with a null in place.

Keywords: left the Join
SELECT [1 Database Name] 1. property name table 1, ......,
. [Database Name n] n property name table n
from [Database Name 1] table 1 left join [Database 2. name] table 2
ON conditional expression;

2, connected to the right outer (right connection)
is the right table as a reference, the display table on the right query field for all records, the left table and the right table shows only matching records, no matching is replaced by null.
Keywords: the Join right;
SELECT [1 Database Name] 1. property name table 1, ......,
[n database name.] N property name table n.
From [Database Name 1] table 1 right join [ 2. database name] table 2
ON condition expressions;
==
SELECT [1 database name] 1. property name table 1, ......,
[database name n] n property name table n..
from [database name 2] table 2 left join [database name 1] table. 1
ON conditional expression;


3, the whole outer connector (fully connected)
about restriction table is not fully connected, all the display data, NULL data is not matched with a place.
Keywords: Full the Join;
MySQL does not support full connectivity;

Use the left and right connections to achieve full connection
connection: union

Five multi-table related query
can query the data connection of a plurality of tables (generally preferably not more than 4 tables)

General format:
. SELECT [1 Database Name] 1. property name table 1, ......, [Database Name n] n property name table n
from [Database Name 1] table 1 inner join [2 database name .] table 2
ON conditional expression
inner join [3. database name] 3 on the conditional expression table
. inner join [database name n] n on the conditional expression table ......;

Sixth, Table alias
Table alias format:
In a query from the table alias watches


Using the difference between two related query multiple tables and table formats give aliases:
SELECT name s.s_name, c.c_name class, cou.cou_name course
from the Join Student Inner class C S = ON s.sc_no c.c_no
Inner the Join Course cou on s.scou_no = cou.cou_no;

select s.s_name 姓名,c.c_name 班级,cou.cou_name 课程
from student s inner join class c inner join course cou
on s.sc_no=c.c_no && s.scou_no=cou.cou_no;

Guess you like

Origin www.cnblogs.com/liugangjiayou/p/11723455.html
Recommended