错误操作数应在逗号后包含1列

时间:2016-12-21 17:07:57

标签: sql

此查询正在运作

SELECT 
    *,
    (SELECT dtm 
     FROM vladimir.chatthreadhistory hi 
     WHERE hi.dtm < ch.created 
     ORDER BY hi.dtm DESC 
     LIMIT 1) AS aa 
FROM 
    vladimir.chatmessage ch 
ORDER BY 
    ch.created DESC;

但如果我想从第二个选择中显示更多,我会收到此错误

   SELECT *,(SELECT dtm,threadid from vladimir.chatthreadhistory hi where hi.dtm <ch.created ORDER BY hi.dtm DESC limit 1)as aa 
FROM vladimir.chatmessage ch order by ch.created DESC;

你能告诉我为什么吗?

1 个答案:

答案 0 :(得分:2)

您需要两个不同的相关子查询。这是一种方式

SELECT *,
       (SELECT dtm
        FROM   vladimir.chatthreadhistory hi
        WHERE  hi.dtm < ch.created
        ORDER  BY hi.dtm DESC limit 1) AS aa,
       (SELECT threadid
        FROM   vladimir.chatthreadhistory hi
        WHERE  hi.dtm < ch.created
        ORDER  BY hi.dtm DESC limit 1) AS threadid
FROM   vladimir.chatmessage ch
ORDER  BY ch.created DESC; 

如果您的DBMS支持与APPLY中的SQL SERVER运算符类似的内容,那么在单个查询中我们可以选择这两列