Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

sql - 使用两个表在SQLplus中给出一个输出(Using Two Tables to give one output in SQLplus)

Table: Person (表:人)

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+

PersonId is the primary key column for this table.

(PersonId是此表的主键列。)

Table: Address (表:地址)

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+

AddressId is the primary key column for this table.

(AddressId是此表的主键列。)

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:

(为报表编写一个SQL查询,该报表将为Person表中的每个人提供以下信息,而不管每个人是否都有地址:)

FirstName, LastName, City, State

So this is a pretty straight forward question and my answer is

(所以这是一个非常简单的问题,我的答案是)

select p.FirstName,p.LastName, a.City, a.State
from Person p , Address a
where p.PersonId = a.PersonId

And I'm not getting anything in the output and yes I filled up the tables with some simple lines of data.

(而且我没有在输出中得到任何东西,是的,我用一些简单的数据行填充了表。)

  ask by fairy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sample data would help, but - the way you described it, it looks as if you need outer join .

(示例数据会有所帮助,但是-按照您描述的方式,它看起来好像您需要外部join 。)

Something like this:

(像这样:)

select p.FirstName, p.LastName, a.City, a.State
from Person p left join Address a on a.personid = p.personid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...