结合两张表person和address

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. 为报表编写一个SQL查询,为Person表中的每个人提供以下信息,而不管这些人是否有地址:
FirstName ,LastName,City ,State

根据条件不管这些人是否有地址,如果用inner join 那么在地址表中,没用地址的人们将不会出现结果中。所以用左外连接。
select p.FirstName,p.lastname,a.city,a.state from person p left join address a on p.personId = a.personId
因为查询字段在两张表中是唯一的,所以可以省略表前缀,写成
select FirstName ,LastName ,city ,state from person left join address on Person.personId = address.personId
上一篇:通过端口映射连接不同网段的oracle


下一篇:如何快速清空项目中的session值