Summary of Leetcode-Mysql topics and knowledge points (175. Combine two tables)

Computer Xiaobai QAQ, because I want to find a few summer internships, I have filled the members and want to focus on the mysql part of leetcode. Write this series of blog posts to communicate with you, and we will continue to update some of the prepared questions in the future. Welcome to communicate with you, and ask the big guys to spray QAQ. Because I have taken many detours as a beginner, I will try my best to write in detail. If you can help future friends, please leave a message to encourage me. Hahahaha.

175. Combine two tables

Topic: Write a SQL query to meet the conditions, regardless of whether the person has address information or not, you need to provide the following information of the person based on the above two tables:

FirstName, LastName, City, State

All are knowledge points, friends!

About table join join: format join (select the connection type) on (represents the condition, the format is table 1. column name = table 2. column name)

Table 1 and Table 2 can be renamed in the format of the original table name as the new table name because some table names are too long QAQ

Join connection method:

In this place, mysql and mssql are slightly different. Let me talk about the common left join, right join and inner join.

In mathematics, we have all learned the concepts of intersection and union. inner join is similar to intersection, that is, returns the common parts of the two tables. The left join completely retains the contents of the first table. Look at the second table. The first table is returned in the same way, and right join is just the opposite. Isn’t that a bit unintelligible hahahaha, let’s give you a chestnut, for example, the id number of the first table is 1, 2, 3, and the second is 2, 3, 4, and then the inner join returns. 2,3, left join returns 1,2,3 right join returns 2,3,4, so it's much clearer QAQ.

The difference is that MSSQL or SQL Server has a full server (the question is why I wrote two QAQs separately)

The full server is similar to the union in the collection, using the previous example means that the returned value is 1, 2, 3, 4.

This function is not available in mysql, but it can also be implemented. The specific method is like this, first join the left, then join the right and then splice the two tables together, in this example, the left join gets 1, 2, 3, the right join gets 2, 3, 4 and then joins together to get 1. ,2,3,4 have the same effect as full join. The function used for splicing together is union. If join is left and right connection, union is up and down connection. This is much clearer. The union here is the splicing method of deduplication, and union all does not deduplicate. If union all is used in the above example, the return value is 1, 2, 3, 2, 3, 4. This is a more complicated sql later. Zhong is one of the pits that are often stepped on (of course, I said nothing, there are many pits, and I only know QAQ if I step on it)

This is just a simple question. I have written so many questions. How many QAQs can I write for more than 100 questions?

Code:

select FirstName, Lastname,City,State

from Person as p1 ##Rename

left join Address as p2 ##Select connection type, rename

on p1.PersonId=p2.PersonId ##条件

result:

{"headers": ["FirstName", "Lastname", "City", "State"], "values": [["Allen", "Wang", null, null]]}##Reserved during left connection The first table has all the information, but there are two items that have no information in the second table, so it will return null. In the previous example, the left join returns 1, 2, 3, but if the select mentions 1, the information is in Table 2. The item will be returned as null.

Guess you like

Origin blog.csdn.net/weixin_43167461/article/details/113102632