操作数应包含1列if子句

时间:2018-09-19 13:59:34

标签: mysql database if-statement select

我不明白为什么这个查询是错误的。我只尝试了对与错查询,它可以工作,但是当我将它们放在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 = 0的条目。

1 个答案:

答案 0 :(得分: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);