“您的SQL语法出错”执行三表连接

时间:2014-09-22 06:02:54

标签: mysql sql jdbc

我有三张桌子

学生

id PK
student_name

id PK
group_name
student_id FK
university

Subject_gkm

id PK
group_id FK
lection_hours

当我进行此查询时

SELECT groups.university, groups.group_name, students.stud_name, subject_gkm.lection_hours
                from groups inner join students
                on groups.student_id = students.id join subject_gkm
                on subject_gkm.group_id = groups.id
                order by groups.university asc;

在mysql 5.5服务器上使用Windows命令行它完全正常,但是当我使用以下代码进行此查询时:

String url = "jdbc:mysql://localhost:3306/";
        String database = "labdb";
        String userName = "root";
        String password = "";


        String select =
                "SELECT groups.university, groups.group_name, students.stud_name, subject_gkm.lection_hours" +
                        "from groups inner join students " +
                        "on groups.student_id = students.id join subject_gkm" +
                        "on subject_gkm.group_id = groups.id" +
                        "order by groups.university asc";

        try (Connection connection = DriverManager.getConnection(url + database, userName, password)) {

            Statement statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery(select);

            while (resultSet.next()) {
                System.out.println("University: " + resultSet.getString("university")
                                + " Group name: " + resultSet.getString("group_name")
                                + " Student name: " + resultSet.getString("stud_name")
                                + " Lection hours: " + resultSet.getTime("lection_hours")
                );
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

我收到此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join students on groups.student_id = students.id join subject_gkmon subjec' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jdbc.Util.getInstance(Util.java:383)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1062)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4140)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2820)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2769)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1569)
    at lab4.main(lab4.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我使用MAVEN作为依赖管理器,我的sql连接器依赖是

 <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
 </dependency>

有人知道为什么会这样吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join students on groups.student_id = students.id join subject_gkmon subjec' at line 1

这告诉你出了什么问题,你有一个语法错误。你不要在on之类的代币之间放置空格。

    String select =
            "SELECT groups.university, groups.group_name, students.stud_name, subject_gkm.lection_hours" + // hoursfrom
                    "from groups inner join students " +
                    "on groups.student_id = students.id join subject_gkm" + // subject_gkmon
                    "on subject_gkm.group_id = groups.id" + // idorder
                    "order by groups.university asc";

要避免此错误,请保持一致:始终在每行上包含前导或尾随空格。