将 2 select 条语句转换为 MySql 内的一条语句

Converting 2 select statements into a single statement within MySql

我正在使用两个子查询从同一个 table 中获取 log_msglog_state。 我想问一下如何将这 2 个子查询转换为单个子查询,这样我就不需要调用相同的 table 两次。

SELECT t1.objectId, t1.name, 
(SELECT a.logs FROM logTable a WHERE a.logId = t1.logId ORDER BY id DESC LIMIT 1) AS log_msg,
(SELECT a.state FROM logTable a WHERE a.logId = t1.logId ORDER BY id DESC LIMIT 1) AS log_state,
FROM table1 t1 WHERE t1.CreateDate >= '2019-12-01';

您可以按照以下方式加入和筛选每个组的顶部日志记录:

select t1.objectId, t1.name, l.logs, l.state
from table1 t1
inner join logTable l
    on l.id = (select max(l1.id) from logTable l1 where l1.logId = t1.id)