select
aid, bid
from
(
select
a.id AS aid, b.id AS bid,
rownum AS no
from
person a, person b
where
a.id < b.id
order by
abs(a.age-b.age)
) t
WHERE
t.no = 1;
注:
person表与自己关联,关联条件为 a.id < b.id
目的是 避免同时出现
3,4 与 4,3
order by abs(a.age-b.age) 为按年龄差距 排序, 从小到大
rownum AS no 为行号, 也就是 从小到大的顺序。
WHERE
t.no = 1;
为获取 最小的那一行.
select r.id, s.id
from person r, person, s
where r.id
from person r, person, s
where r.id