执行查询时出现 LIMIT 错误
P粉668146636
P粉668146636 2024-03-27 12:26:04
0
1
392

这就是我尝试编写的查询

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)

这是错误

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粉668146636
P粉668146636

全部回复(1)
P粉276876663

您必须使用 IN 而不是 ÈXISTS。

exists 的语法不同

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

这将检查是否有一个最喜欢的帖子 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板