[LeetCode] 175.Combine Two TablesTable: Person
Table: Address
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people: FirstName, LastName, City, State LeetCode also has a question about database. Let's do it. This is the first question. It is relatively simple. It is a question of joint search between two tables. We need to use Join operation. For some Join operations, you can read my previous blog SQL Left Join, Right Join, Inner Join, and Natural Join. The most direct way is to use Left Join to join the two tables according to PersonId: Solution 1: SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId; When using Left Join, we can also use the Using keyword to declare which column name we want to use for the join: Solution 2: SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person LEFT JOIN Address USING(PersonId); Or we can add the Natural keyword so that we don't have to declare specific columns and MySQL can search for the same columns by itself: Solution 3: SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person NATURAL LEFT JOIN Address; References: https://leetcode.com/discuss/21216/its-a-simple-question-of-left-join-my-solution-attached https://leetcode.com/discuss/53001/comparative-solution-between-left-using-natural-left-join This is the end of this article about SQL implementation of LeetCode (175. Join two tables). For more relevant SQL implementation of joining two tables, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Specific use of Node.js package manager npm
>>: HTML background color gradient achieved through CSS
Menu bar example 1: Copy code The code is as foll...
Table of contents Preface cause Phenomenon why? A...
Here is a Vue single sign-on demo for your refere...
Table of contents Preface Global Lock Full databa...
Table of contents Preface Lua Script nignx.conf c...
This article is based on the Windows 10 system en...
Table of contents 1. Troubleshooting and locating...
1. The value used in the button refers to the text...
Sometimes some docker containers exit after a per...
Table of contents Preface What is index pushdown?...
When configuring nginx reverse proxy, the slashes...
Several typical values of innodb_flush_method f...
When a website is maliciously requested, blacklis...
I remember a question the interviewer asked durin...
1. Find a suitable version of redis for docker Yo...