Likou SQL Brush Question 1

topic

Insert picture description here
Answer: When the
database returns records by connecting two or more tables, it will generate an intermediate temporary table, and then return this temporary table to the user. When using left jion, the difference between on and where conditions is as follows:

1. The on condition is a condition used when generating a temporary table. Regardless of whether the condition in on is true, it will return the records in the table on the left.

2. The where condition is a condition for filtering the temporary table after the temporary table is generated. At this time, there is no left join meaning (must return the records of the left table), and the conditions are not true and all are filtered out.

Two tables are given in the title, and a combination is required.
The requirement for display after the combination is that regardless of whether there is address information in the PersonId in the Person table, the FirstName, LastName, City and State based on the information in the Person and Address tables must be displayed. It
can be seen that the relationship between the two tables Yes: Person is the main table and Address is the secondary table.
There are three types of connections between two tables, inner connection, left connection and right connection.
According to the requirements of the topic, the key point is to display all the PersonIds in the main table, and whether the corresponding City and State information is in the corresponding attached table is no longer the key point.
After the above analysis, using left join… on meets the requirements of the topic.

select a.FirstName ,a.LastName,b.City, b.State
from Person a
LEFT JOIN Address b
on a.PersonId =  b.PersonId;

Guess you like

Origin blog.csdn.net/Txixi/article/details/115259835