表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
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
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
-----------------------------------
--测试数据
浦东 21 41
闵行区 20 42
徐汇区 22 30
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
怎么理解啊?