日期:2014-05-17  浏览次数:20360 次

一个team表中有一组数据abcd四个球队,用一个语句查询有多少组比赛
请用SQL写出来,谢!

------解决方案--------------------
select * from tb a,tb b, tb c ,tb d where a.col<b.col and a.col<c.col and a.col<d.col
------解决方案--------------------
是这个样子吗?????
SQL code
WITH cte AS
(
SELECT 'A' AS team
UNION ALL SELECT 'B'
UNION ALL SELECT 'C'
UNION ALL SELECT 'D'
)
SELECT * FROM cte a,cte b
WHERE a.team<b.team 
/*
team    team
A    B
A    C
A    D
B    C
B    D
C    D
*/

------解决方案--------------------
SQL code
IF object_id('tc') IS NOT NULL 
  DROP TABLE tc 

GO

CREATE TABLE tc (
  team VARCHAR(10))

INSERT INTO tc
SELECT 'A'
UNION ALL
SELECT 'B'
UNION ALL
SELECT 'C'
UNION ALL
SELECT 'D'

SELECT Count(1)
FROM   tc a
       INNER JOIN tc b
         ON a.team < b.team