MySql连接排序?

MySql连接排序?,mysql,Mysql,我有两张表作业,笔记 我想输出状态为status='lost'的作业列表,以及该作业的最近的注释(基于注释创建日期) 我的问题是: select jobs.id, jobs.name, jobs.status inner join notes on jobs.id=notes.jobId where jobs.status='lost' group by jobs.id order by notes.createDate DESC 我本以为输出会显示给定作业的最新注释。但它显示了该工作的第一个

我有两张表
作业,笔记

我想输出状态为
status='lost'
的作业列表,以及该作业的最近的注释(基于注释创建日期)

我的问题是:

select jobs.id, jobs.name, jobs.status
inner join notes on jobs.id=notes.jobId
where jobs.status='lost'
group by jobs.id
order by notes.createDate DESC
我本以为输出会显示给定作业的最新注释。但它显示了该工作的第一个注意事项。我把sort从DESC改为ASC,只是为了看看会发生什么,结果是一样的

然后我尝试在主选择框中嵌套一个选择框,它挂起了


这应该很容易,而且我确信它是..我遗漏了什么?

解决这个问题的方法很多,但您可以使用子查询

select jobs.id, jobs.name, jobs.status
(select noteField from notes on jobs.id=notes.jobId order by createDate desc limit 1) note
where jobs.status='lost'

当我在一条类似的船上时,我求助于在连接上使用子查询:

select jobs.id, jobs.name, jobs.status
from jobs
inner join notes on jobs.id = notes.jobId
   and notes.createDate = (select max(notes.createDate)
                          from notes
                          where jobs.id = notes.createDate
                          group by notes.jobId)
where jobs.status='lost'
group by jobs.id
order by notes.createDate DESC

实际上,它返回任意(不确定)结果。您缺少手册,手册中有一整章都是关于此主题的。手册页:。。。还有一个类似问题的答案可以帮助你: