首页 > 解决方案 > 如何使用 Jetbrains Exposed 库通过左连接添加单行?

问题描述

我需要在单个查询中获取所有聊天以及最新的聊天消息。

我有一个 SQL 查询:

SELECT * FROM chats 
    // here I obtain latest message uid for chat
    LEFT OUTER 
        JOIN ( SELECT
                    chat_uid, 
                    MAX(timestamp) AS last_message_ts 
        FROM chat_messages 
        GROUP BY chat_messages.chat_uid) AS subq 
        ON subq.chat_uid = chats.chat_uid
    // here I obtain latest message for chat
    LEFT OUTER 
        JOIN chat_messages AS msgs
        ON msgs.chat_uid = subq.chat_uid 
        AND msgs.timestamp = subq.last_message_ts

但现在我想把它转换成Exposed Kotlin代码。

而且我不知道如何在这里使用多个左外连接。有什么想法吗?

标签: mysqlkotlin-exposed

解决方案


推荐阅读