日期:2014-05-17 浏览次数:20614 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-05-13 13:47:06
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[tel] int)
insert [A]
select 101,133133 union all
select 101,144144
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([id] int,[address] varchar(4))
insert [B]
select 101,'add1' union all
select 101,'add2' union all
select 101,'add3'
--------------开始查询--------------------------
SELECT B.ID ,
[ADDRESS] ,
A.TEL
FROM ( SELECT B.* ,
ROW_NUMBER() OVER ( ORDER BY ID, [ADDRESS] ) PID
FROM B
) B
LEFT JOIN ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ID, TEL ) PID
FROM A
) A ON B.PID = A.PID
----------------结果----------------------------
/*
ID