SQL_STUDY:7.SQL JOIN

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/NumberOneStudent/article/details/102719492

sql join full join

SQL JOIN

SQL join according to a relationship between two or more columns in a table, query data from these tables.

Join 和 Key

Sometimes in order to get complete results, we need to get results from two or more tables. We need to perform join.

Table in the database can be linked to each other through the key. The primary key (Primary Key) is a column value of each row in this column are unique. In the table, each primary key values ​​are unique. The purpose of this will not be repeated in the case of all of the data in each table, together with the cross-binding between data tables.

See "Persons" table:
P_Id LastName FirstName Address City
1 John Adams Oxford Street London
2 George Bush Fifth Avenue New York
3 Thomas Carter Street Beijing Changan Zip
Please note, "Id_P" column is the primary key of the table Persons. This means that no two rows can have the same Id_P. Even if two people are exactly the same name, Id_P can also distinguish between them.

Next, look at "Orders" table:
ID_o of the OrderNo P_Id
. 1. 3 77895
2 44678. 3
. 3 22456. 1
. 4 24562. 1
. 5 65 34764
Note, "Id_O" Orders table column is the primary key, and, "Orders" table "Id_P" column used to refer to people "persons" table without using their exact names.

Please note, "Id_P" column of the above table two link up.


References two tables

We can get data from two tables by reference two tables:

Who ordered a product, and what they ordered product?

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P 

The results set:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678


SQL JOIN - 使用 Join

In addition to the above method, we can also use the data acquired from the keyword JOIN two tables.

If we want to list all ordered, you can use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName

The results set:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678

Different SQL JOIN
addition INNER JOIN (en) we use the example above, we can use several other connections.

Listed below are the types of JOIN you can use, and the differences between them.

  • INNER JOIN two must match before returning
  • LEFT JOIN: even if there is no match in the right table, also returns all rows from the left table
  • RIGHT JOIN: even if there is no match left table, also returns all rows from the right table
  • FULL JOIN: As long as there is a match where a table return line

Guess you like

Origin blog.csdn.net/NumberOneStudent/article/details/102719492