我不明白为什么这个查询是错误的。我只尝试了对与错查询,它可以工作,但是当我将它们放在if子句中时,它就没有效果。在此先感谢您的帮助:)

SELECT
IF (
    (SELECT useronline FROM wp_livechat ORDER BY id DESC LIMIT 1) = 1
    ,
    (SELECT *
     FROM wp_livechat
     WHERE id >= (SELECT id
                  FROM wp_livechat
                  WHERE useronline = 0
                  ORDER BY id DESC
                  LIMIT 0, 1)
     AND id <= (SELECT id
                FROM wp_livechat
                WHERE useronline = 1
                ORDER BY id DESC
                LIMIT 0, 1)
    ),
    (SELECT *
     FROM wp_livechat
     WHERE id >= (SELECT id
                  FROM wp_livechat
                  WHERE useronline = 0
                  ORDER BY id DESC
                  LIMIT 1, 1)
     AND id <= (SELECT id
                FROM wp_livechat
                WHERE useronline = 0
                ORDER BY id DESC
                LIMIT 0, 1)
    )
);


编辑:这是表:https://ibb.co/huri6e

而且我想从最后一个useronline = 0到最前一个useronline = 0获得消息。并且如果最后一个条目是useronline = 1,那么我想从带有useronline = 1的最后一个条目检索消息到带有useronline = 1的最后一个条目useronline = 0。

最佳答案

我得到这个查询。不管怎么说,多谢拉 :)

SELECT *
FROM wp_livechat
WHERE ID > (
    SELECT IF (
    (SELECT useronline FROM wp_livechat ORDER BY ID DESC LIMIT 1) = 1,
        (SELECT ID
        FROM wp_livechat
        WHERE useronline = 0
        ORDER BY id DESC LIMIT 0, 1)
    ,
        (SELECT ID
        FROM wp_livechat
        WHERE useronline = 0
        ORDER BY id DESC LIMIT 1, 1)
    )
)
AND ID <= (SELECT ID FROM wp_livechat WHERE useronline = 1 ORDER BY ID DESC LIMIT 1);

10-05 17:40