一个sql面试题

来源:百度知道 编辑:UC知道 时间:2024/06/11 03:40:00
一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.

select t1.name,t2.name from department t1,department t2 where t1.name<t2.name;

********
测试log:

[TEST@ORA1] SQL>select t1.name,t2.name from department t1,department t2 where t1.name<t2.name;

N N
- -
a b
a c
a d
b c
b d

---
以上,希望对你有所帮助。

举一个例子给楼主参照

declare @T table(Col nvarchar(1))
insert @T select 'a'
union all select 'b'
union all select 'c'
union all select 'd'

select
*
from @T a,@T b
where a.col<b.Col
order by 1

(4 个资料列受到影响)
Col Col
---- ----
a b
a c
a d
b c
b d
c d

(6 个资料列受到影响)