3

我需要帮助....我不擅长 SQL 尝试应用 JOIN 时出现此错误:

[ Token line number = 1,Token line offset = 66,Token in error = JOIN ]

这是我的 SQL:

var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
                "FROM Team" +
                "LEFT JOIN Fixtures" +
                "ON Team.TeamId=Fixtures.HomeTeam" +
                "ORDER BY Team.TeamName";

Team Table 有 PK:TeamId Fixture Table 有 FK:HomeTeam 我正在使用 WebMatrix 2. Razor WebPages

4

3 回答 3

3

行连接之间没有空格。更改每一行以在末尾包含空格。

var query = "SELECT Team.TeamName, Fixtures.HomeTeam " +
            "FROM Team " +
            "LEFT JOIN Fixtures " +
            "ON Team.TeamId=Fixtures.HomeTeam " +
            "ORDER BY Team.TeamName";
于 2012-12-17T17:54:28.263 回答
1

正如 Charles Brentana 所指出的,您错过了 SQL 命令中的空格。

也许更好的解决方案是您使用逐字字符串文字,即在双引号字符之前使用 @ 字符创建的字符串,它可以跨越多行:

var query = @"SELECT Team.TeamName, Fixtures.HomeTeam
                FROM Team
                LEFT JOIN Fixtures
                ON Team.TeamId=Fixtures.HomeTeam
                ORDER BY Team.TeamName";
于 2012-12-18T08:01:03.460 回答
0

你的字符串之间需要空格。

我通过将空格作为第一个字符来避免这种情况因此当您忘记对其进行编码时,这一点非常明显:

var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
            " FROM Team" +
            " LEFT JOIN Fixtures" +
            " ON Team.TeamId=Fixtures.HomeTeam" +
            " ORDER BY Team.TeamName";

如果您始终以这种方式编码,您将能够立即发现任何缺失的空格。

于 2012-12-18T08:09:57.680 回答