2017-09-14 141 views
0

如何顯示印度與巴基斯坦,印度與斯里蘭卡,印度與孟加拉國,孟加拉國與斯里蘭卡,孟加拉國與巴基斯坦,巴基斯坦與斯里蘭卡在下表中的結果?比較SQL Server表中的同一行

Teams 
------- 
India 
Pakistan 
Sri Lanka 
Bangladesh 

例如:

--Output: 
Bangladesh vs. India 
Bangladesh vs. Sri Lanka 
Bangladesh vs. Pakistan 
India vs. Sri Lanka 
India vs. Pakistan 
Pakistan vs. Sri Lanka 

回答

1

我猜你正在尋找非重複隊比賽的組合。請嘗試以下:

CREATE TABLE temp 
(
    Team VARCHAR(100) 
) 


INSERT INTO temp 
VALUES ('India'), 
('Bangladesh'), 
('Sri Lanka'), 
('Pakistan') 


SELECT t1.team + ' vs. ' + t2.team 
FROM temp t1 
CROSS JOIN temp t2 
where t1.Team < t2.Team 


DROP TABLE temp 

    Result 
    ----------- 
    India vs. Sri Lanka 
    India vs. Pakistan 
    Bangladesh vs. India 
    Bangladesh vs. Sri Lanka 
    Bangladesh vs. Pakistan 
    Pakistan vs. Sri Lanka