mysql-错误 - 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法

时间:2022-05-31 22:52:16

I have two tables in my database viz linkrecord(URL,NAME) and dishrate(dishname, rate,review). I want to create a 3rd table viz record which contains URL, dishname and rating from the 1st two table,in correspondance with dishname which is common to both table. I have tried the following Insert query but it shows the error: "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 '.NAME = dishrate.dishnameORDER BY dishrate.rate DESC' at line 1"

我的数据库中有两个表viz linkrecord(URL,NAME)和dishrate(dishname,rate,review)。我想创建一个第3个表viz记录,其中包含来自前两个表的URL,dishname和rating,与两个表共有的dishname相对应。我尝试了以下插入查询,但它显示错误:“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以获得正确的语法使用'.NAME = dishrate.dishnameORDER BY dishrate.rate DESC'在第1行附近“

The query is:

查询是:

    String query= "INSERT INTO crawler.record (URL, Dishname, rate)"+
                           "SELECT linkrecord.URL, dishrate.dishname,dishrate.rate"+
                            "FROM linkrecord, dishrate"+
                            "WHERE linkrecord.NAME = dishrate.dishname"+ 
                            "ORDER BY dishrate.rate DESC";
            Statement stmt=db.conn.createStatement();
            stmt.executeUpdate(query);

I am unable to find the error in the above query.What should I do? Thank You

我无法在上面的查询中找到错误。我该怎么办?谢谢

1 个解决方案

#1


3  

You forget the spaces and you ended up with query parts like:

你忘了这些空格,最后得到的查询部分如下:

INSERT INTO crawler.record (URL, Dishname,rate)<space missing here>SELECT

INSERT INTO crawler.record(URL,Dishname,rate) <此处缺少的空间> SELECT

Correct way is:

正确的方法是:

String query= "INSERT INTO crawler.record (URL, Dishname, rate) "+
                           "SELECT linkrecord.URL, dishrate.dishname,dishrate.rate "+
                            "FROM linkrecord, dishrate "+
                            "WHERE linkrecord.NAME = dishrate.dishname "+ 
                            "ORDER BY dishrate.rate DESC";

#1


3  

You forget the spaces and you ended up with query parts like:

你忘了这些空格,最后得到的查询部分如下:

INSERT INTO crawler.record (URL, Dishname,rate)<space missing here>SELECT

INSERT INTO crawler.record(URL,Dishname,rate) <此处缺少的空间> SELECT

Correct way is:

正确的方法是:

String query= "INSERT INTO crawler.record (URL, Dishname, rate) "+
                           "SELECT linkrecord.URL, dishrate.dishname,dishrate.rate "+
                            "FROM linkrecord, dishrate "+
                            "WHERE linkrecord.NAME = dishrate.dishname "+ 
                            "ORDER BY dishrate.rate DESC";