0

我有表客户(id,name)电话(id,customerId,phoneType,phone)是否可以编写一个返回如下行的查询:

客户 ID、客户名称、电话 1、电话 2、移动 1、移动 2

Phone1,Phone2,Mobile1,Mobile2 是电话类型

我试过

select id as CustomerId, name as CustomerName, phone as Phone1
from Customers, Phones
where Customers.id = Phones.customerId and 
Phones.phoneType = N'Phone1'
4

1 回答 1

1

使用枢轴

select *
from (select id as customerid, name as customername, phonetype, phone
     from customers inner join phones on customers.id = phones.customerid
) src
pivot
(max(phone) for phonetype in ([phone1],[phone2],[mobile1],[mobile2])) p
于 2012-10-02T09:55:26.377 回答