Home  >  Q&A  >  body text

LIMIT error when executing query

This is the query I tried to write

select posts.*  ,users.* 
FROM posts
INNER JOIN users
   ON posts.user_id = users.id
   WHERE posts.user_id != '27'
   AND posts.pid EXISTS(SELECT post_id FROM favourites WHERE posts.pid=favourites.post_id)

This is an error

select posts.*  ,users.* 
FROM posts
INNER JOIN users
   ON posts.user_id = users.id
   WHERE posts.user_id != '27'
   AND posts.pid EXISTS(SELECT post_id FROM favourites WHERE posts.pid=favourites.post_id) LIMIT 0, 25
MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'EXISTS(SELECT post_id FROM favourites WHERE posts.pid=favourites.post_id) LIM...' at line 6

P粉668146636P粉668146636206 days ago362

reply all(1)I'll reply

  • P粉276876663

    P粉2768766632024-03-28 09:09:56

    You must use IN instead of ÈXISTS.

    The syntax of exists is different

    SELECT 
        posts.*, users.*
    FROM
        posts
            INNER JOIN
        users ON posts.user_id = users.id
    WHERE
        posts.user_id != '27'
            AND posts.pid IN (SELECT 
                post_id
            FROM
                favourites
            WHERE
                posts.pid = favourites.post_id)
    LIMIT 0 , 25

    This will check if there is a favorite post id

    SELECT 
        posts.*, users.*
    FROM
        posts
            INNER JOIN
        users ON posts.user_id = users.id
    WHERE
        posts.user_id != '27'
            AND EXISTS (SELECT 
                post_id
            FROM
                favourites
            WHERE
                posts.pid = favourites.post_id)
    LIMIT 0 , 25

    reply
    0
  • Cancelreply