SQL-W3School- High: SQL RIGHT JOIN Keyword

ylbtech-SQL-W3School- High: SQL RIGHT JOIN Keyword

 

1. Back to top
1、

SQL RIGHT JOIN Keyword

RIGHT JOIN keyword will be right table (table_name2) where all rows are returned, even though in the left table (table_name1) no matching rows .

RIGHT JOIN keyword syntax

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2 
ON table_name1.column_name=table_name2.column_name

Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN.

The original table (used in the example):

"Persons" 表:

Id_P LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

"Orders" 表:

Id_O OrderNo Id_P
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 65

Right connection (RIGHT JOIN) Example

Now we want to list all the orders and those who ordered them - if any.

You can use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT 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
    34764

RIGHT JOIN keyword from the right table (Orders) where all rows are returned, even though in the left table (Persons) is not a match.

2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
1、
2、
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/11809986.html