0

我遇到了一些唠叨。为了从我的一个 drupal SQL 数据库中获取一些数据,我需要一个相当精细的查询。问题是在控制台/MysqlWorkBench 中查询工作正常,而在我使用 JDBC 的 JAVA 程序中它根本不起作用。

原始查询是:

    # Tag use from the perspective of all tag entries belonging to nodes in table 'field_data_field_topic'

    SELECT fdf.entity_type, node.nid, td.name, td.tid, ti.created, users.uid
    FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td
    ON(fdf.field_topic_tid = td.tid)
    LEFT OUTER JOIN taxonomy_index ti
    ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid)
    LEFT OUTER JOIN node
    ON(fdf.entity_id = node.nid)
    LEFT OUTER JOIN field_data_body fd
    ON(fd.entity_id = node.nid)
    LEFT OUTER JOIN users
    ON(users.uid = node.uid)


    #Restricting to content from 01-02-2012 up to 11-06-2012
    WHERE node.created > 1328054400
    #Restricting to either Nodes or Comments
    AND fdf.entity_type = "node"
    #Restricting to node status=1
    AND node.status=1
    #Remove defective node with nid=1594
    AND node.nid not like "1594"

    UNION

    # Tag use from the perspective of all tag entries belonging to comments in table 'field_data_field_topic'

    SELECT fdf.entity_type, comment.cid, td.name, td.tid, ti.created, users.uid
    FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td
    ON(fdf.field_topic_tid = td.tid)
    LEFT OUTER JOIN taxonomy_index ti
    ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid)
    LEFT OUTER JOIN comment
    ON(fdf.entity_id = comment.cid)
    LEFT OUTER JOIN field_data_comment_body fc
    ON(comment.cid=fc.entity_id)
    LEFT OUTER JOIN users
    ON(users.uid = comment.uid)

    #Restricting to content from 01-02-2012 up to 11-06-2012
    WHERE comment.created > 1328054400
    #Restricting to Comments
    AND fdf.entity_type = "comment"
    #Restricting to node status=1
    AND comment.status=1

在Java中,我放了:

String sql = "SELECT fdf.entity_type, node.nid, td.name, td.tid, ti.created, users.uid "
        + "FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td " +
        "ON(fdf.field_topic_tid = td.tid) " +
        "LEFT OUTER JOIN taxonomy_index ti " +
        "ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) " +
        "LEFT OUTER JOIN node " +
        "ON(fdf.entity_id = node.nid) " +
        "LEFT OUTER JOIN field_data_body fd " +
        "ON(fd.entity_id = node.nid) " +
        "LEFT OUTER JOIN users " +
        "ON(users.uid = node.uid " +
        "WHERE node.created > 1328054400 " +
        "AND fdf.entity_type = 'node' " +
        "AND node.status = 1 " +
        "AND node.nid not like '1594' " +
            "UNION " +
                "SELECT fdf.entity_type, comment.cid, td.name, td.tid, ti.created, users.uid " +
                "FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td " +
                "ON(fdf.field_topic_tid = td.tid) " +
                "LEFT OUTER JOIN taxonomy_index ti " +
                "ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) " +
                "LEFT OUTER JOIN comment " +
                "ON(fdf.entity_id = comment.cid) " +
                "LEFT OUTER JOIN field_data_comment_body fc " +
                "ON(comment.cid=fc.entity_id) " +
                "LEFT OUTER JOIN users " +
                "ON(users.uid = comment.uid) " +
                "WHERE comment.created > 1328054400 " +
                "AND fdf.entity_type = 'comment' " +
                "AND comment.status=1"
                ;

我得到的错误是:

MySQL Database connection established
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 'WHERE node.created > 1328054400 AND fdf.entity_type = "node" AND node.status = 1' at line 1**
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2310)
    at pp.orientTest.Orient4GraphUsersCreatedTagsTaggedNodes.main(Orient4GraphUsersCreatedTagsTaggedNodes.java:51)
MySQL Database connection terminated
4

2 回答 2

0
fdf.entity_type = "node", 

这应该是单引号

像:

fdf.entity_type = 'node'
于 2012-08-30T14:59:15.383 回答
0

看起来这一行缺少一个右括号:

    "ON(users.uid = node.uid " +
于 2012-08-30T15:03:39.730 回答