一个sql面试题

时间:2022-06-23 19:56:05
一个表,字段和内容如下:

name

a
b
c
d

假设这是个球队的队名表,每个球队要与其它三支队进行一次比赛,请用一个sql语句列出比赛的组合

6 个解决方案

#1


每个球队要与其它三支队进行一次比赛

总的每次是四对,你总数也是四对,不就刚好一组吗?还是你提供的数据有问题?

#2



-- 这个意思?
SQL> with t as(
  2  select 'a' name from dual
  3  union all
  4  select 'b' from dual
  5  union all
  6  select 'c' from dual
  7  union all
  8  select 'd' from dual
  9  )
 10  select t1.name Blue,t2.name Green from t t1 join t t2 on t1.name<t2.name;

BL GR
-- --
a  b
a  c
a  d
b  c
b  d
c  d

#3


--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([name] [nvarchar](10))
INSERT INTO [tb]
SELECT 'a' UNION ALL
SELECT 'b' UNION ALL
SELECT 'c' UNION ALL
SELECT 'd'

--SELECT * FROM [tb]

-->SQL查询如下:
select a.name+' VS '+b.name as nvn from tb a ,tb b where a.name<b.name
/*
nvn
------------------------
a VS b
a VS c
a VS d
b VS c
b VS d
c VS d

(6 行受影响)
*/
受猫大侠的点醒,也猜一下

#4


--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([name] varchar(1))
insert [ta]
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

--------------------------------查询开始------------------------------

select  a.name+char(32)+'vs'+char(32)+b.name 
from [ta] a  cross join [ta] b
where a.name<>b.name
/*
------
a vs b
a vs c
a vs d
b vs a
b vs c
b vs d
c vs a
c vs b
c vs d
d vs a
d vs b
d vs c

(12 行受影响)


*/

#5


是比赛一场呀
--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([name] varchar(1))
insert [ta]
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

--------------------------------查询开始------------------------------

select  a.name+char(32)+'vs'+char(32)+b.name 
from [ta] a  , [ta] b
where a.name<b.name
/*
------
a vs b
a vs c
a vs d
b vs c
b vs d
c vs d

(6 行受影响)

*/

#6


2,3楼可行.

#1


每个球队要与其它三支队进行一次比赛

总的每次是四对,你总数也是四对,不就刚好一组吗?还是你提供的数据有问题?

#2



-- 这个意思?
SQL> with t as(
  2  select 'a' name from dual
  3  union all
  4  select 'b' from dual
  5  union all
  6  select 'c' from dual
  7  union all
  8  select 'd' from dual
  9  )
 10  select t1.name Blue,t2.name Green from t t1 join t t2 on t1.name<t2.name;

BL GR
-- --
a  b
a  c
a  d
b  c
b  d
c  d

#3


--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([name] [nvarchar](10))
INSERT INTO [tb]
SELECT 'a' UNION ALL
SELECT 'b' UNION ALL
SELECT 'c' UNION ALL
SELECT 'd'

--SELECT * FROM [tb]

-->SQL查询如下:
select a.name+' VS '+b.name as nvn from tb a ,tb b where a.name<b.name
/*
nvn
------------------------
a VS b
a VS c
a VS d
b VS c
b VS d
c VS d

(6 行受影响)
*/
受猫大侠的点醒,也猜一下

#4


--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([name] varchar(1))
insert [ta]
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

--------------------------------查询开始------------------------------

select  a.name+char(32)+'vs'+char(32)+b.name 
from [ta] a  cross join [ta] b
where a.name<>b.name
/*
------
a vs b
a vs c
a vs d
b vs a
b vs c
b vs d
c vs a
c vs b
c vs d
d vs a
d vs b
d vs c

(12 行受影响)


*/

#5


是比赛一场呀
--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([name] varchar(1))
insert [ta]
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

--------------------------------查询开始------------------------------

select  a.name+char(32)+'vs'+char(32)+b.name 
from [ta] a  , [ta] b
where a.name<b.name
/*
------
a vs b
a vs c
a vs d
b vs c
b vs d
c vs d

(6 行受影响)

*/

#6


2,3楼可行.