SQL查询从键表中选择多个条目到同一行的不同列中
Posted
技术标签:
【中文标题】SQL查询从键表中选择多个条目到同一行的不同列中【英文标题】:SQL query selecting multiple entries from key tables into different columns of the same line 【发布时间】:2019-05-08 20:13:11 【问题描述】:我正在尝试创建一个 SQL 查询,它可以从表中选择多个条目并将这些条目输入到同一行的不同列中。
例子:
表1“人”
|---------------------|------------------|------------------|
| PersonID | FirstName | LastName |
|---------------------|------------------|------------------|
| 1 | John | Smith |
|---------------------|------------------|------------------|
| 2 | Jane | Doe |
|---------------------|------------------|------------------|
表 2“电话类型”
|---------------------|------------------|
| PhoneTypeID | Name |
|---------------------|------------------|
| 1 | Home |
|---------------------|------------------|
| 2 | Work |
|---------------------|------------------|
| 3 | Mobile |
|---------------------|------------------|
表 3“电话号码”
|---------------------|------------------|------------------|
| PersonID | PhoneTypeID | PhoneNumber |
|---------------------|------------------|------------------|
| 1 | 1 | 12300 |
|---------------------|------------------|------------------|
| 2 | 1 | 45600 |
|---------------------|------------------|------------------|
| 2 | 2 | 78900 |
|---------------------|------------------|------------------|
SQL 查询的预期结果:
|------------------|---------------|---------------|---------------|
| FirstName | LastName |HomePhoneNumber|WorkPhoneNumber|
|------------------|---------------|---------------|---------------|
| John | Smith | 12300 | NULL |
|------------------|---------------|---------------|---------------|
| Jane | Doe | 45600 | 78900 |
|------------------|---------------|---------------|---------------|
我想从 PhoneNumber 表中选择家庭和工作电话号码,并将它们写在同一行的不同列中。
查询应该看起来像这样:
SELECT Person.FirstName, Person.LastName, PhoneNumber.PhoneNumber AS HomePhoneNumber, PhoneNumber.PhoneNumber AS WorkPhoneNumber
FROM Person
LEFT OUTER JOIN PhoneNumber ON Person.PersonID = PhoneNumber.PersonID
LEFT OUTER JOIN PhoneType ON PhoneType.PhoneTypeID = PhoneNumber.PhoneTypeID
WHERE PhoneType.PhoneTypeID = 'Home'
OR PhoneType.PhoneTypeID = 'Work'
【问题讨论】:
【参考方案1】:您可以在 join 子句中指定 PhoneTypeID
SELECT Person.FirstName, Person.LastName, HomePhone.PhoneNumber AS HomePhoneNumber, WorkPhone.PhoneNumber AS WorkPhoneNumber
FROM Person
LEFT OUTER JOIN PhoneNumber HomePhone ON Person.PersonID = HomePhone.PersonID
and HomePhone.PhoneTypeID = 1
LEFT OUTER JOIN PhoneNumber WorkPhone ON Person.PersonID = WorkPhone.PersonID
and WorkPhone.PhoneTypeID = 2
http://sqlfiddle.com/#!9/9cf19c2/1
【讨论】:
这样您可以为手机号码添加另一个加入以完成可能性【参考方案2】:你可以使用correlated subquery
作为
select FirstName,LastName,
( select PhoneNumber
from PhoneNumber
where PhoneTypeID = 1 and PersonID = p.PersonID ) as HomePhoneNumber,
( select PhoneNumber
from PhoneNumber
where PhoneTypeID = 2 and PersonID = p.PersonID ) as WorkPhoneNumber
from person p;
或将conditional aggregation
与left join
一起使用
select FirstName,LastName,
max( case when PhoneTypeID = 1 then n.PhoneNumber end ) as HomePhoneNumber,
max( case when PhoneTypeID = 2 then n.PhoneNumber end ) as WorkPhoneNumber
from person p
left join PhoneNumber n
on n.PersonID = p.PersonID
group by FirstName,LastName
Demo
【讨论】:
我想过,但我认为这不会奏效,是吗?查询如何知道哪个 PhoneNumber 属于哪个 Person? “从 PhoneType ID = 1 的 PhoneNumber 中选择 PhoneNumber”将返回该类型的所有 PhoneNumber。不是属于那个人的 谢谢!是的,确实,编辑后的版本效果很好:)以上是关于SQL查询从键表中选择多个条目到同一行的不同列中的主要内容,如果未能解决你的问题,请参考以下文章