此查询正在运作
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;
你能告诉我为什么吗?
答案 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
运算符类似的内容,那么在单个查询中我们可以选择这两列