0

私はちょっとしつこいことに出くわしました。drupal SQL データベースの 1 つからデータを取得するには、非常に複雑なクエリが必要です。問題は、console/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

この行には閉じ括弧がありません:

    "ON(users.uid = node.uid " +
于 2012-08-30T15:03:39.730 に答える
0
fdf.entity_type = "node", 

これは一重引用符で囲む必要があります

お気に入り:

fdf.entity_type = 'node'
于 2012-08-30T14:59:15.383 に答える