路过的进来帮忙看下
表A
buyid sellid qu
1 2 浦东区
3 4 闵行区
5 6 徐汇区
表B
peopleid brithday
1 1986.9.24
2 1966.6.6
3 1987.8.4
4 1965.2.4
5 1985.3.21
6 1977.12.1
说明:buyid(买家) 和sellid(卖家) 分别和peopleid 相连.
结果
------------------
区域 买家年龄 卖家年龄
---------------------------
浦东区 当前时间-1986.9.24 当前时间-1966.6.6
闵行区 当前时间-1987.8.4 当前时间-1965.2.4
徐汇区 当前时间-1985.3.21 当前时间-1977.12.1
[解决办法]
create table #A (
buyid int,
sellid int,
qu varchar(20))
insert #A
select 1,2, '浦东 '
union all
select 3,4, '闵行区 '
union all
select 5,6, '徐汇区 '
create table #B (
peopleid int,
birthday varchar(20))
insert #B
select 1, '1986.9.24 '
union all
select 2, '1966.6.6 '
union all
select 3, '1987.8.4 '
union all
select 4, '1965.2.4 '
union all
select 5, '1985.3.21 '
union all
select 6, '1977.12.1 '
select '区域 ' = qu, '买家年龄 '=datediff(year,convert(datetime,B1.birthday),getdate()),
'卖家年龄 '=datediff(year,convert(datetime,B2.birthday),getdate())
from #A,#B B1,#B B2
where #A.buyid = B1.peopleid and #A.sellid = B2.peopleid
drop table #A
drop table #B
-----------------------------------
--测试数据
浦东2141
闵行区2042
徐汇区2230
[解决办法]
select qu,c.birthday as 买家年龄 ,d.birthday as 卖家年龄
from
(select a.sellid,a.qu,b.brithday from a,b where a.buyid = b.peopleid ) c,(select a.sellid,a.qu,b.brithday from a,b where a.sellid = b.peopleid) d
where c.sellid = d.sellid