0

以下の 2 つのクエリを交差させようとしていますが、次のエラーが発生します。

ProgrammingError: (ProgrammingError) (1064、「SQL 構文にエラーがあります。「INTERSECT SELECT user_checkpoint.id AS user_checkpoint_id, user_checkpoint.user_」行の近くで使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。 4") 'SELECT anon_1.user_checkpoint_id AS anon_1_user_checkpoint_id, anon_1.user_checkpoint_user_id AS anon_1_user_checkpoint_user_id, anon_1.user_checkpoint_checkpoint_id AS anon_1_user_checkpoint_checkpoint_id, anon_1.user_checkpoint_date_added AS anon_1_user_checkpoint_date_added \nFROM (SELECT user_checkpoint.id AS user_checkpoint_id, user_checkpoint.user_id AS user_checkpoint_user_id, user_checkpoint.checkpoint_id AS user_checkpoint_checkpoint_id,ユーザー_チェックポイント。date_added AS user_checkpoint_date_added \nFROM user_checkpoint INNER JOIN checkpoint ON checkpoint.id = user_checkpoint.checkpoint_id INNER JOIN user AS user_1 ON user_checkpoint.user_id = user_1.id INNER JOIN facebook_user AS facebook_user_1 ON facebook_user_1.id = user_1.facebook_user_id INNER JOIN friend_connection ON friend_connection.fb_user_to = facebook_user_1.id INNER JOIN facebook_user ON facebook_user.id = friend_connection.fb_user_from INNER JOIN user ON user.facebook_user_id = facebook_user.id \nWHERE user.id != %s AND checkpoint.creator = user_1.id AND checkpoint.demo = %s INTERSECT SELECT user_checkpoint.id AS user_checkpoint_id, user_checkpoint.user_id AS user_checkpoint_user_id, user_checkpoint.checkpoint_id AS user_checkpoint_checkpoint_id, user_checkpoint.date_added AS user_checkpoint_date_added \nFROM user_checkpoint INNER JOIN checkpoint ON checkpoint.id = user_checkpoint.checkpoint_id \nWHERE checkpoint.creator != %s AND checkpoint.demo = %s) AS anon_1 ORDER BY checkpoint.date_created DESC \n LIMIT %s' (185L 、0、185L、0、1)

    #get checkpoints from users with friends
    FriendUserCheckpoint, FriendFacebookUser, FriendUser = aliased(UserCheckpoint), aliased(FacebookUser), aliased(User)
    q = (db.session.query(UserCheckpoint).
         join(Checkpoint, Checkpoint.id == UserCheckpoint.checkpoint_id).
         join(FriendUser, FriendUser.id == UserCheckpoint.user_id).
         join(FriendFacebookUser, FriendFacebookUser.id == FriendUser.facebook_user_id).
         join(FriendConnection, FriendConnection.fb_user_to == FriendFacebookUser.id).
         join(FacebookUser, FacebookUser.id == FriendConnection.fb_user_from).
         join(User, User.facebook_user_id == FacebookUser.id).
         filter(User.id != user_obj.id).
         filter(Checkpoint.creator == FriendUser.id).
         filter(Checkpoint.demo == False)
         )

    #get checkpoints from users without friends
    p = (db.session.query(UserCheckpoint).
         join(Checkpoint, Checkpoint.id == UserCheckpoint.checkpoint_id).
         filter(Checkpoint.creator != user_obj.id).
         filter(Checkpoint.demo == False)
         )

    w = q.intersect(p)

    return w.order_by(desc(Checkpoint.date_created)).limit(limit).all()

どうすれば修正できますか?ありがとう!

4

1 に答える 1

0

クエリの問題は次のorder_byとおりです。クエリにない列で並べ替えようとしています。結果のSQLクエリは次のようになります。

SELECT anon_1.col1, anon_1.col2, ...
FROM (
    SELECT  <UserCheckpoint columns>
    FROM    UserCheckpoint
    ... (joins, filters etc)


    INTERSECT

    SELECT  <UserCheckpoint columns>
    FROM    UserCheckpoint
    ... (joins, filters etc)
) anon_1
ORDER BY checkpoint.date_created DESC -- @note: here is the problem

ご覧のとおり、外側のクエリcheckpointのソース テーブルのリストにはテーブルがありません (これにはtable anon_1が 1 つしか含まれていません)。を削除するorder_byと、クエリが機能すると思います。

order_by が必要な場合は、date_created列も返すようにクエリを変更し、結果を並べ替えて、後でその列を削除します。

q = (db.session.query(UserCheckpoint, Checkpoint.date_created).
...
p = (db.session.query(UserCheckpoint, Checkpoint.date_created).
...
return [uc for (uc, _) in w.order_by(desc(Checkpoint.date_created)).limit(limit).all()]
于 2012-05-21T14:15:29.657 に答える