[LeetCode]-DataBase-Combine Two Tables

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

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

需求:简单的表连接

查询:sql

CREATE TABLE Person(
PersonId TINYINT UNSIGNED auto_increment PRIMARY KEY,
FirstName varchar(20),
LastName varchar(20)
)ENGINE=MyISAM CHARSET=utf8;
CREATE TABLE Address(
AddressId TINYINT UNSIGNED auto_increment PRIMARY KEY,
PersonId TINYINT UNSIGNED,
City VARCHAR(20),
State VARCHAR(20)
)ENGINE=MyISAM CHARSET=utf8;

SELECT t1.FirstName,t1.LastName,t2.City,t2.State
FROM Person t1
LEFT JOIN Address t2 ON t1.PersonId=t2.PersonId

				
时间: 2024-08-08 03:48:23

[LeetCode]-DataBase-Combine Two Tables的相关文章

LeetCode:Combine Two Tables - 跨表查询

1.题目名称 Combine Two Tables(跨表查询) 2.题目地址 https://leetcode.com/problems/combine-two-tables/ 3.题目内容 现在有两张表Person和Address,它们的表结构如下: 表Person: +-------------+---------+ | Column Name | Type    | +-------------+---------+ | PersonId    | int     | | FirstNam

LeetCode 175 Combine Two Tables mysql,left join 难度:0

https://leetcode.com/problems/combine-two-tables/ Combine Two Tables Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+

【leetcode】Combine Two Tables

突然发现leetcode出sql题了,找了道最简单的,找找自信.. 题目如下: Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key c

LeetCode SQL: Combine Two Tables

SELECT FirstName, LastName, City, State FROM Person p LEFT JOIN Address a ON p.PersonId = a.PersonId 居然出SQL的题目了,左连接就行

sql 中多表查询-leetcode : Combine Two Tables

因为对数据库的内容早都忘得差不多了,所以我的第一感觉是: select Person.FirstName, Person.LastName, Address.City from Person, Address where Person.PersonId=Address.PersonId 结果出错了: 因为至少这个人是存在的,只是没有她的地址,你不至于搜不到吧, 但是我这种写法是引用两个表,从两个表中获取数据,就是找两者相同的情况下的结果. 使用join 可以连接两个表: join: 如果表中有一

LeetCode 175 Combine Two Tables

Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table: Address +--

LeeCode(Database)-Combine Two Tables

Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table: Address +--

LeetCode-Algorithms #001 Two Sum, Database #175 Combine Two Tables

最近两周一直感觉学习比较松懈, 打算加大一点强度, 从今天开始, 希望能在每天正常进度完成后在LeetCode上选一两题写一写, 同时学习一下高手的做法. LeetCode - Algorithms #001 Two Sum 给定一个整数数组, 找出其中两个元素, 使其和等于目标值, 返回这两个元素在原数组中的索引组成的数组. 可以假设有且只有一组正解, 且每个元素只能使用一次. 我的思路: 其实想不到什么好的方法, 遍历就好, 非常素朴的回答: 1 class Solution { 2 pub

leetcode database题目

LeetCode有10道SQL的题目,最近学习SQL语言,顺便刷题强化一下, 说实话刷完SQL学习指南这本书,不是很难,上面的例子 跟语法规则我都能理解透, 实际中来做一些比较难的业务逻辑题,却一下子很难想起如何利用SQL来描述 还是要多刷题开阔自己的思路,把学到手的语法加以练习 应用到实际中去 197. Rising Temperature Given a Weather table, write a SQL query to find all dates' Ids with higher t

leetcode database解题记录

175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p left join Address a on p.PersonId=a.PersonId; 7个case耗时1001ms(注意耗时多次提交会不同,一般相差不大,偶尔也有相差大的)   --2015/1/11 176 Second Highest Salary 题目:写一条sql语句查询Employee