赞
踩
问题:
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
解决办法:
通过左关联 表A left join 表2 on 相同字段
左关联(Left Join)就是以左边的表为准,只要左边的表有数据就输出,不管右边的表有没有数据。右关联(Right Join)相反,以右边的表为准。而内关联则是两张表都要有数据才能查询到。外关联则是只要任意一张表有数据就能查询并显示。
不能用where操作 ;如果使用where的话如果右表不存在匹配的行,那么不会返回数据,所以用Left join。
select FirstName, LastName, City, State from Person left join Address on Person.PersonId=Address.PersonId;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。