0

我正在尝试查询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 和对手中选择 2 个学校(int)。
  2. 从这两所学校中选择每个权重的所有初学者('True')。
  3. 根据体重将每所学校的首发球员配对。
  4. 能够选择每一对并操作数据。

感谢任何帮助!

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

4

1 回答 1

1

这可能是您的查询。您可以多次声明同一个表,但您需要为每个表提供不同的别名。在这种情况下,玩家被列为 p1 和 p2 两次。玩家权重的匹配也在 join 部分完成,但可能在 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
于 2012-04-06T00:32:14.787 回答