SQL Server表联盟

时间:2011-09-26 13:13:20

标签: sql sql-server

我想从比赛(得分和回合)建立一个表联赛。

我从我的SQL Server数据库中获取计算每个团队和比赛的积分,但我现在希望生成一个表联盟..(足球)

有人可以帮我吗?

SELECT     TOP (100) PERCENT dbo.jos_joomleague_matches.match_id, dbo.jos_joomleague_clubs.name AS Team, jos_joomleague_clubs_1.name AS Team2, 
                      dbo.jos_joomleague_matches.match_date, dbo.jos_joomleague_matches.matchpart1_result, dbo.jos_joomleague_matches.matchpart2_result, 
                      dbo.jos_joomleague_rounds.name, 
                      CASE WHEN dbo.jos_joomleague_matches.matchpart1_result > dbo.jos_joomleague_matches.matchpart2_result THEN 3 WHEN dbo.jos_joomleague_matches.matchpart1_result
                       = dbo.jos_joomleague_matches.matchpart2_result THEN 1 ELSE 0 END AS ptsclub1HOME, 
                      CASE WHEN dbo.jos_joomleague_matches.matchpart2_result > dbo.jos_joomleague_matches.matchpart1_result THEN 3 WHEN dbo.jos_joomleague_matches.matchpart2_result
                       = dbo.jos_joomleague_matches.matchpart1_result THEN 1 ELSE 0 END AS ptsclub2AWAY
FROM         dbo.jos_joomleague_rounds INNER JOIN
                      dbo.jos_joomleague_clubs AS jos_joomleague_clubs_1 INNER JOIN
                      dbo.jos_joomleague_matches INNER JOIN
                      dbo.jos_joomleague_clubs ON dbo.jos_joomleague_matches.matchpart1 = dbo.jos_joomleague_clubs.id ON 
                      jos_joomleague_clubs_1.id = dbo.jos_joomleague_matches.matchpart2 ON dbo.jos_joomleague_rounds.id = dbo.jos_joomleague_matches.round_id
ORDER BY dbo.jos_joomleague_matches.match_date

`

1 个答案:

答案 0 :(得分:3)

在创建表格之前,先从问题的自然语言描述开始。想想关系:

league有很多teamsmatch有两个teams在给定的date上播放,并且具有特定的outcome

我看到三个表:leagueteammatch。从那些开始。

如果您已经考虑过查询,那么您还为时过早。首先获取基本表结构。