zoukankan      html  css  js  c++  java
  • [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
    
    +-------------+---------+
    | 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查询,报表能为person表中的每个人提供FirstName、LastName、City、和State,无论这些人是否有地址:

     解法一:

    SELECT a.FirstName, a.LastName,  b.City, b.State FROM Person as a 
    LEFT JOIN Address as b 
    on a.PersonId = b.PersonId;
    

      

    解法二:

      可以使用关键字NATURAL,这样我们就不要申明具体的列了,MySQL可以自行搜搜相同的列:

    SELECT a.FirstName, a.LastName, b.City, b.State 
    FROM Person as a 
    NATURAL LEFT JOIN  Address as b; 
    

      

    解法三:
      在使用LEFT JOIN时,我们也可以使用关键字USING来申明我们想用哪个列名来进行联合:

    SELECT a.FirstName, a.LastName, b.City, b.State 
    FROM Person as a 
    LEFT JOIN  Address as b
    USING(PersonId); 
    

      

                  
    申明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    P1486 [NOI2004]郁闷的出纳员
    poj2155 Matrix
    [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
    分布式相关学习整理
    分布式学习之JTA
    linux相关命令整理
    每日笔记-redis的理解及相关应用
    利用redis实现分布式锁知识点总结及相关改进
    每日笔记-类加载机制及相关拓展
    多线程学习系列-概述
  • 原文地址:https://www.cnblogs.com/lsyb-python/p/11113271.html
Copyright © 2011-2022 走看看