Sql - 如何进行此查询

时间:2012-04-05 23:59:50

标签: sql sql-server-ce

我正在尝试查询 SQL Compact

基本上是这些查询,但我希望我可以将它们组合成一个。

1

SELECT schoolId,opponent 
FROM TEAM_SCHEDULE
WHERE seasonId = " & i & "
AND gameid = " & currentGame & "
ORDER BY id"

These are variables, trying to eliminate them at some point
currentMatch(0) = schoolId
currentMatch(1) = opponent

2

SELECT id
FROM player
WHERE school = " & currentMatch(0) & "
AND starter = 'TRUE'
AND game_id = " & currentGame & "
ORDER BY weight

3

SELECT id
FROM player
WHERE school = " & currentMatch(1) & "
AND starter = 'TRUE'
AND game_id = " & currentGame & "
ORDER BY weight

我以为我可以做这样的事情,但我遇到了一个问题,试图弄清楚如何将两个学校添加到查询中。

        cmd.CommandText = "SELECT b.id, player.id" &
                            " FROM player b" &
                            " WHERE player.school = " & currentMatch(0) &
                            " OR player.school = " & currentMatch(1) &
                            " INNER JOIN player" &
                            " ON b.weight = player.weight" &
                            " ORDER BY player.weight"

为了解决任何困惑,我想:

  1. 从team_schedule,schoolId和opponent中选择2所学校(int)。
  2. 从这两所学校的每个体重中选择所有开始者('真'')。
  3. 根据体重对每所学校的初学者进行配对。
  4. 能够选择每一对并操纵数据。
  5. 感谢任何帮助!

    这是您的查询Nikola的解析错误, Error

1 个答案:

答案 0 :(得分:1)

这可能是您的查询。您可以多次声明同一个表,但是您需要为每个表提供不同的别名。在这种情况下,播放器列为p1和p2两次。玩家权重的匹配也在联接部分中完成,但它可能在where子句中。

SELECT 
  schoolId,
  opponent, 
  p1.id Player1, 
  p1.Weight Player1Weight, 
  p2.id Player2, 
  p2.Weight Player2Weight
FROM TEAM_SCHEDULE
inner join player p1
   on TEAM_SCHEDULE.schoolId = p1.school
 and TEAM_SCHEDULE.GameID = p1.Game_id
 AND p1.starter = 'TRUE'
inner join player p2
   on TEAM_SCHEDULE.opponent = p2.school
  and TEAM_SCHEDULE.GameID = p2.Game_id
  AND p2.starter = 'TRUE'
-- tricky part - do weights match perfectly 
-- or do you need additional weight range table?
  AND p1.weight = p2.weight
WHERE TEAM_SCHEDULE.seasonId = " & i & "
AND TEAM_SCHEDULE.gameid = " & currentGame & "
ORDER BY id