select a.name, c.content
from 表1 as a,
(select user_id, max(log_time) as lt from 表2 group by ) b,
表2 c
where a.user_id=b.user_id and b.user_id=c.user_id and b.lt=c.log_time
select a.name, max(b.log_time) from 表1 as a, 表2 as b
where a.id = b.user_id group by a.name
根据您最新的需求,SQL修改如下,保证查询效率没的说:
select
a.id,
a.name,
max(b.log_time),
(
select c.content
from 表2 as c where c.user_id = a.id
order by id desc limit 0,1
) as content
from 表1 as a, 表2 as b where a.id = b.user_id group by a.id, a.name
解释:因为您给出的log_time截止到日了,这意味着某天出现多条记录的话,这个日期就说明不了问题了,那我们不妨通过走id倒序的捷径,来取出最新的评论内容。
select a.name,b.log_time from 表1 as a, 表2 as b
where a.id = b.user_id order by b.log_time desc limit 1;