zoukankan      html  css  js  c++  java
  • leetcode 175. Combine Two Tables

    传送门

    175. Combine Two Tables

    My Submissions
    Total Accepted: 16436 Total Submissions: 48308 Difficulty: Easy

    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
    

    Subscribe to see which companies asked this question

    1 # Write your MySQL query statement below
    2 select FirstName, LastName, City, State from
    3     Person left join Address
    4         on Person.PersonId = Address.PersonId

     

    LEFT JOIN

    SQL LEFT JOIN 关键字

    SQL LEFT JOIN 关键字

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。

    LEFT JOIN 关键字语法

    SELECT column_name(s)
    FROM table_name1
    LEFT JOIN table_name2 
    ON table_name1.column_name=table_name2.column_name
    

    注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。

    原始的表 (用在例子中的):

    "Persons" 表:

    Id_PLastNameFirstNameAddressCity
    1 Adams John Oxford Street London
    2 Bush George Fifth Avenue New York
    3 Carter Thomas Changan Street Beijing

    "Orders" 表:

    Id_OOrderNoId_P
    1 77895 3
    2 44678 3
    3 22456 1
    4 24562 1
    5 34764 65

    左连接(LEFT JOIN)实例

    现在,我们希望列出所有的人,以及他们的定购 - 如果有的话。

    您可以使用下面的 SELECT 语句:

    SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
    FROM Persons
    LEFT JOIN Orders
    ON Persons.Id_P=Orders.Id_P
    ORDER BY Persons.LastName
    

    结果集:

    LastNameFirstNameOrderNo
    Adams John 22456
    Adams John 24562
    Carter Thomas 77895
    Carter Thomas 44678
    Bush George  

    LEFT JOIN 关键字会从左表 (Persons) 那里返回所有的行,即使在右表 (Orders) 中没有匹配的行。

  • 相关阅读:
    关于链表的代码
    c++中的友元函数
    javaweb笔记全套
    包装类、object、单例模式、final、抽象类
    Linux变量内容的删除、代替与替换
    2014年工作中遇到的20个问题:181-200
    Qt中 QString 和int,double等的转换
    jsp学习笔记总结
    工作日志2014-07-04
    Maple入门使用教程
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5196034.html
Copyright © 2011-2022 走看看